LoginSignup
34
21

More than 5 years have passed since last update.

freeコマンドの結果を読むのが意外と大変です

Posted at

はじめに

サーバのメモリ使用状況を確認するfreeコマンドの結果が理解しづらかったのでメモを残します。

freeコマンド

freeコマンド
# free
              total        used        free      shared  buff/cache   available
Mem:        1008152      131472      256228      130960      620452      544692
Swap:             0           0           0

早速ですが、CentOS7.4でのfreeコマンドの結果です。今回はIDCFクラウドのLight.S1上に作ったサーバのため、メモリ容量は1GBです。出力結果の項目が幾つかありますが、man freeでマニュアルを読んでも日本語では殆ど何も書いていなかったのですが、ためしに英語のマニュアルを読んだところ説明がありました。

日本語のマニュアル
説明
       free                                                     はシステムの物理メモリとスワップメモリそれぞれに対して、
       使用量と空き容量を表示し、カーネルが用いているバッファも表示する。
       共有メモリの欄は無視してほしい。これは古い機能の名残である。
英語のマニュアル
DESCRIPTION
       free  displays  the  total amount of free and used physical and swap memory in the system, as well as the buffers
       and caches used by the kernel. The information is gathered by parsing /proc/meminfo. The displayed columns are:

       total  Total installed memory (MemTotal and SwapTotal in /proc/meminfo)

       used   Used memory (calculated as total - free - buffers - cache)

       free   Unused memory (MemFree and SwapFree in /proc/meminfo)

       shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on kernels 2.6.32, displayed as  zero  if
              not available)

       buffers
              Memory used by kernel buffers (Buffers in /proc/meminfo)

       cache  Memory used by the page cache and slabs (Cached and Slab in /proc/meminfo)

       buff/cache
              Sum of buffers and cache

       available
              Estimation  of  how  much  memory is available for starting new applications, without swapping. Unlike the
              data provided by the cache or free fields, this field takes into account page cache and also that not  all
              reclaimable  memory  slabs  will  be  reclaimed  due to items being in use (MemAvailable in /proc/meminfo,
              available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

以下、英語のマニュアルを参考にします。totalは全物理メモリを表しているようです。1008152はほぼ1GBなので、ここは納得です。続いてusedtotalからfreebuffercacheを除いたもの、とあります。各項目を見ると、freeは利用されていないメモリ、buff/cacheがbuffersとcacheの合計とあります。

buffersとchacheがそれぞれ何を意味するかを理解するために調べたところ、cacheについては下記のページがわかりやすかったのでポイントとなる箇所を訳してみます。
What is the difference between Buffers and Cached columns in /proc/meminfo output?
ファイル入出力はすべてページキャッシュを介して行われる。書き込みの際にはページキャッシュ上のデータに「dirty」フラグが付けられ、定期的にディスクへの書き込みが行われる。読み込みの際にはページキャッシュのデータの読み込みが行われ、ページキャッシュにデータが存在しない時はディスクからページキャッシュにデータが読み込まれる。現在のLinuxシステムではメモリの逼迫が起きるまではページキャッシュは増え続ける。

bufferについても記載があるのですが、こちらは良く理解できなかったので今後の課題に残します。Cacheに比べると重要度は低いと書いてあるので、一旦は良いかなと。。。以降しばらくはbuff/cache≒cacheと思っておきます。

ここまでで、
total: 全メモリ
free: 全く利用されていないメモリ
buff/cache: キャッシュとして利用されているけど、解放できる可能性のあるメモリ
used: = total - free - buff/cache
です。

ではavailableは何かと言うと、スワップせずに利用可能と見積もられたメモリ、となっています。buff/cacheの中には現在まさに使われているものがあるだろうから、それは除いて計算しているということでしょうか。これも後日要勉強です。

運用上はいかにしてスワップしているかどうかが大事となってきますので、たくさん書いてきましたがavailableさえ気をつけていれば良い、という結論になりそうです。

おわりに

・buffer(ページキャッシュではないほう)とは何か
・availableがfree + buff/cacheよりも低い値となるのは何故か
の2点が理解できずに残ってしまったので、引き続き勉強していこうと思います。また実際に監視アプライアンスでどのように監視するべきかも後日やってみたいです。何も知らなかったらfreeの量だけ見てしまうとか、ありそう!

34
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
21