0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

メモリの使用状況

Last updated at Posted at 2024-05-21

メモリの使用、空き状況を調べる

root@***********:***********# free
              total        used        free      shared  buff/cache   available
Mem:        3907956      859316     1674624        5556     1374016     2798072
Swap:       1190340           0     1190340

total

システムに搭載されている全メモリの量

free

見かけ上の空きメモリ

buff/cashe

バッファキャッシュ、およびページキャッシュが利用するメモリ。システムの空きメモリが減少してきたら、カーネルによって解放される。

available

実質的な空きメモリ。freeフィールドの値に、空きメモリが足りなくなってきたら解放できるカーネル内メモリ領域のサイズを足したもの

used

システムが使用中のメモリ(total-free)からbuff/cacheを引いたもの

気づき

参考書の図を見るとavailableはカーネルが使用中の開放可能なメモリ領域とfreeのメモリ領域を足したものだった。
メモリの使用状況がわかるな。

used

usedの値は、プロセスが使うメモリとカーネルが使うメモリの両方を含みます。
...
usedの値は、プロセスのメモリ使用量に従って増えます。その一方で、プロセスが終了すると、カーネルは当該プロセスのメモリをすべて開放します

root@***********:***********# ./memuse.py 
メモリ獲得前のシステム全体のメモリ使用量を表示します。
              total        used        free      shared  buff/cache   available
Mem:        3907956      859296     1674372        5560     1374288     2798064
Swap:       1190340           0     1190340
メモリ獲得後のシステム全体のメモリ空き容量を表示します。
              total        used        free      shared  buff/cache   available
Mem:        3907956      897852     1635816        5560     1374288     2759508
Swap:       1190340           0     1190340
root@***********:***********# free
              total        used        free      shared  buff/cache   available
Mem:        3907956      887032     1646408        5548     1374516     2770336
Swap:       1190340           0     1190340
root@***********:***********# free
              total        used        free      shared  buff/cache   available
Mem:        3907956      887024     1646408        5548     1374524     2770344
Swap:       1190340           0     1190340
root@***********:***********# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.2  0.2 102116 11632 ?        Ss   11:22   0:01 /sbin/init s
...
root        4022  0.0  0.0   9928  3236 pts/0    R+   11:30   0:00 ps aux
root@***********:***********# free
              total        used        free      shared  buff/cache   available
Mem:        3907956      855240     1678164        5544     1374552     2802204
Swap:       1190340           0     1190340

気づき

あまり変わっていない。けどその後freeをすると使用中のメモリがプロセス開始前と同じぐらいになった。

buff/cache

ページキャッシュおよびバッファキャッシュに使われるメモリの量の値を表します。
...
ページキャッシュとバッファキャッシュはアクセス速度が遅いストレージデバイス上にあるファイルのデータをアクセス速度を上げるためのカーネル機能です。ここでは「ストレージデバイス上にあるファイルのデータを読み出すと、メモリ上にデータをキャッシュしておく(貯めておく)」ということを覚えていただければ結構です。

root@***********:***********# ./buff-cache.sh 
ファイル作成前のシステム全体のメモリ使用量を表示します。
              total        used        free      shared  buff/cache   available
Mem:        3907956      874492     1657316        5556     1376148     2782892
Swap:       1190340           0     1190340
1GBのファイルを新規作成します。これによってカーネルはメモリ上に1GBのページキャッシュ領域を獲得します。
1024+0 レコード入力
1024+0 レコード出力
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.22534 s, 333 MB/s
ページキャッシュ獲得後のシステム全体のメモリ使用量を表示します。
              total        used        free      shared  buff/cache   available
Mem:        3907956      878688      575268        5556     2454000     2764092
Swap:       1190340           0     1190340
ファイル削除後、つまりページキャッシュ削除後のシステム全体のメモリ使用量を表示します。
              total        used        free      shared  buff/cache   available
Mem:        3907956      947760     1583452        5556     1376744     2709384
Swap:       1190340           0     1190340

気づき

1GBのファイルを作成したらキャッシュの方に1GB増えた

sar -s

メモリに関する統計情報を得られます。
...
freeコマンドに比べると1行に情報がまとまっているので継続的に情報を取得するような時に使い勝手がいいです。

root@***********:***********# sar -r 1 5
Linux 5.15.0-107-generic (linuxstructure2) 	2024年05月21日 	_x86_64_	(1 CPU)

11時41分22秒 kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
11時41分23秒   1614732   2741400    850628     21.77     47892   1269096   4206752     82.51    516328   1501312       288
11時41分24秒   1614732   2741408    850620     21.77     47892   1269104   4206764     82.51    516336   1501364       304
11時41分25秒   1614732   2741408    850620     21.77     47892   1269104   4206764     82.51    516336   1501364       304
11時41分26秒   1614732   2741408    850620     21.77     47892   1269104   4206764     82.51    516336   1501364       304
11時41分27秒   1614732   2741408    850620     21.77     47892   1269104   4206764     82.51    516336   1501364       304
平均値:    1614732   2741406    850622     21.77     47892   1269102   4206762     82.51    516334   1501354       301

気づき

連続的なメモリの使用状況を見ることができる

出典

感想

メモリの状況を見ることができるから嬉しい。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?