メモリ使用率を取得する方法(Linux、freeコマンド)

Linuxfreeコマンドでメモリ使用率を取得する方法を紹介します。

環境

[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@centos8 ~]#

1. freeコマンドとメモリ使用率

freeコマンドを実行すると以下のように結果が表示されます。

[root@centos8 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        2037136      852272      536336       12572      648528     1020348
Swap:       1679356           0     1679356
[root@centos8 ~]#

出力結果の意味は以下となります。

項目 意味
total インストールされているメモリの合計
used 使用済みメモリ(「total - free - buffers - cache」として計算)
free 未使用のメモリ
shared tmpfsに使用されているメモリ
buff/cache バッファとキャッシュの合計
(buffers) カーネルバッファによって使用されるメモリ
(cache) ページキャッシュとスラブによって使用されるメモリ
available 実質的な空きメモリ(free + buff/cacheのうち開放可能な領域)

図で表すと以下のようになります。

f:id:yasushi-jp:20210923180911p:plain
メモリの内訳

従って、メモリ使用率は

メモリ使用率(%)= ( ( total - available ) ÷ total ) × 100

となります。

2. freeコマンドの実行結果をファイルに出力

1秒間隔でfreeコマンドの実行結果を表示させる場合、以下のコマンドを実行します。

free -s 1

[root@centos8 ~]# free -s 1
              total        used        free      shared  buff/cache   available
Mem:        2037136      873692      475712       12556      687732      998536
Swap:       1679356           0     1679356

              total        used        free      shared  buff/cache   available
Mem:        2037136      873692      475712       12556      687732      998536
Swap:       1679356           0     1679356

              total        used        free      shared  buff/cache   available
Mem:        2037136      873692      475712       12556      687732      998536
Swap:       1679356           0     1679356

              total        used        free      shared  buff/cache   available
Mem:        2037136      873692      475712       12556      687732      998536
Swap:       1679356           0     1679356

freeコマンドの実行結果に「時刻」を付けて表示させる場合、以下のコマンドを実行します。

free -s 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 }'

[root@centos8 ~]# free -s 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 }'
2021/09/23 17:57:42               total        used        free      shared  buff/cache   available
2021/09/23 17:57:42 Mem:        2037136      874180      475224       12556      687732      998048
2021/09/23 17:57:42 Swap:       1679356           0     1679356
2021/09/23 17:57:43
2021/09/23 17:57:43               total        used        free      shared  buff/cache   available
2021/09/23 17:57:43 Mem:        2037136      874180      475224       12556      687732      998048
2021/09/23 17:57:43 Swap:       1679356           0     1679356
2021/09/23 17:57:44
2021/09/23 17:57:44               total        used        free      shared  buff/cache   available
2021/09/23 17:57:44 Mem:        2037136      874180      475224       12556      687732      998048
2021/09/23 17:57:44 Swap:       1679356           0     1679356
2021/09/23 17:57:45
2021/09/23 17:57:45               total        used        free      shared  buff/cache   available
2021/09/23 17:57:45 Mem:        2037136      874180      475224       12556      687732      998048
2021/09/23 17:57:45 Swap:       1679356           0     1679356

ファイル(ここでは「free.log」)に出力する場合、以下のコマンドを実行します。 パイプをリフレッシュするためにsystem(":")を使用します。

free -s 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { system(":") }' > free.log

[root@centos8 log]# free -s 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { system(":") }' > free.log
[root@centos8 log]# cat free.log
2021/09/23 17:58:41               total        used        free      shared  buff/cache   available
2021/09/23 17:58:41 Mem:        2037136      874240      475164       12556      687732      997988
2021/09/23 17:58:41 Swap:       1679356           0     1679356
2021/09/23 17:58:42
2021/09/23 17:58:42               total        used        free      shared  buff/cache   available
2021/09/23 17:58:42 Mem:        2037136      874532      474864       12556      687740      997692
2021/09/23 17:58:42 Swap:       1679356           0     1679356
2021/09/23 17:58:43
2021/09/23 17:58:43               total        used        free      shared  buff/cache   available
2021/09/23 17:58:43 Mem:        2037136      874532      474864       12556      687740      997692
2021/09/23 17:58:43 Swap:       1679356           0     1679356
2021/09/23 17:58:44
2021/09/23 17:58:44               total        used        free      shared  buff/cache   available
2021/09/23 17:58:44 Mem:        2037136      874564      474832       12556      687740      997660
2021/09/23 17:58:44 Swap:       1679356           0     1679356
2021/09/23 17:58:45
2021/09/23 17:58:45               total        used        free      shared  buff/cache   available
2021/09/23 17:58:45 Mem:        2037136      874532      474864       12556      687740      997692
2021/09/23 17:58:45 Swap:       1679356           0     1679356
[root@centos8 log]#

長時間、実行結果を取得する場合、バックグラウンドで動かして、ログアウトしても終了しないようにしたいこともあります。 その場合、nohupを使用して以下のコマンドを実行します。

nohup bash -c "free -s 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" < /dev/null > free.log 2>&1 &

[root@centos8 log]# nohup bash -c "free -s 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" < /dev/null > free.log 2>&1 &
[1] 3067
[root@centos8 log]#

出力ファイル名に時刻を持たせたい場合、以下のコマンドを実行します。

nohup bash -c "free -s 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" < /dev/null > free_$(date "+%Y%m%d_%H%M%S").log 2>&1 &

[root@centos8 log]# nohup bash -c "free -s 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" < /dev/null > free_$(date "+%Y%m%d_%H%M%S").log 2>&1 &
[1] 3317
[root@centos8 log]# ls
free_20210923_181216.log
[root@centos8 log]# cat free_20210923_181216.log
21/09/23 18:12:16               total        used        free      shared  buff/cache   available
21/09/23 18:12:16 Mem:        2037136      874460      474736       12556      687940      997764
21/09/23 18:12:16 Swap:       1679356           0     1679356
21/09/23 18:12:17
21/09/23 18:12:17               total        used        free      shared  buff/cache   available
21/09/23 18:12:17 Mem:        2037136      875056      474136       12556      687944      997168
21/09/23 18:12:17 Swap:       1679356           0     1679356
21/09/23 18:12:18
21/09/23 18:12:18               total        used        free      shared  buff/cache   available
21/09/23 18:12:18 Mem:        2037136      875056      474136       12556      687944      997168
21/09/23 18:12:18 Swap:       1679356           0     1679356
21/09/23 18:12:19
21/09/23 18:12:19               total        used        free      shared  buff/cache   available
21/09/23 18:12:19 Mem:        2037136      875056      474136       12556      687944      997168
21/09/23 18:12:19 Swap:       1679356           0     1679356
21/09/23 18:12:20
21/09/23 18:12:20               total        used        free      shared  buff/cache   available
21/09/23 18:12:20 Mem:        2037136      875056      474136       12556      687944      997168
21/09/23 18:12:20 Swap:       1679356           0     1679356
21/09/23 18:12:21
21/09/23 18:12:21               total        used        free      shared  buff/cache   available
21/09/23 18:12:21 Mem:        2037136      875056      474136       12556      687944      997168
21/09/23 18:12:21 Swap:       1679356           0     1679356
21/09/23 18:12:22
21/09/23 18:12:22               total        used        free      shared  buff/cache   available
21/09/23 18:12:22 Mem:        2037136      875056      474136       12556      687944      997168
21/09/23 18:12:22 Swap:       1679356           0     1679356
[root@centos8 log]#

以上