メモリ使用率を取得する方法(Linux、freeコマンド)
Linuxのfree
コマンドでメモリ使用率を取得する方法を紹介します。
環境
[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のうち開放可能な領域) |
図で表すと以下のようになります。
従って、メモリ使用率は
メモリ使用率(%)= ( ( 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]#
以上
CPU使用率を取得する方法(Linux、vmstatコマンド)
Linuxのvmstat
コマンドでCPU使用率を取得する方法を紹介します。
環境
[root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) [root@centos8 ~]#
1. vmstatコマンド実行
vmstat
コマンドを実行すると以下のように結果が表示されます。
[root@centos8 ~]# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 481508 2216 688484 0 0 361 65 229 402 2 2 93 4 0 [root@centos8 ~]#
出力結果の意味は以下となります。
項目1 | 項目2 | 値の意味 |
---|---|---|
procs | r | ランタイム待ちのプロセス数 |
b | 割り込み不可能なスリープ状態にあるプロセス数 | |
memory | swpd | 使用中の仮想メモリの量(KB) |
free | 空きメモリの量(KB) | |
buff | バッファとして使用しているメモリの量(KB) | |
cache | キャッシュに使用しているメモリの量(KB) | |
swap | si | ディスクからスワップインしているメモリの量(KB/s) |
so | ディスクにスワップアウトしているメモリの量(KB/s) | |
io | bi | ブロックデバイスから受け取ったブロック数(blocks/s) |
bo | ブロックデバイスに送られたブロック数(blocks/s) | |
system | in | クロック割り込みも含む、1秒あたりの割り込み回数 |
cs | 1秒あたりのコンテキストスイッチの回数 | |
cpu | us | カーネルコード以外の実行に使用した時間(ユーザー時間、nice 時間を含む)(%) |
sy | カーネルコードの実行に使用した時間(システム時間)(%) | |
id | アイドル時間(%) | |
wa | IO 待ち時間(%) | |
st | 仮想マシンから盗まれた時間(%) |
2. CPU使用率
vmstat
コマンドの結果のcpuの項目(us、sy、id、wa、st)を足すと100
になります。
従って、CPU使用率は
CPU使用率(%)=100 - idの値
となります。
3. vmstatコマンドの実行結果をファイルに出力
1秒間隔でvmstat
コマンドの実行結果を表示させる場合、以下のコマンドを実行します。
vmstat 1
[root@centos8 ~]# vmstat 1 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 479484 2216 689364 0 0 83 16 181 349 1 1 98 1 0 0 0 0 479424 2216 689364 0 0 0 0 165 338 0 0 100 0 0 0 0 0 479424 2216 689364 0 0 0 0 163 335 0 1 99 0 0 0 0 0 479424 2216 689364 0 0 0 0 162 333 0 1 99 0 0 0 0 0 479424 2216 689364 0 0 0 0 183 339 1 1 98 0 0 0 0 0 479424 2216 689364 0 0 0 0 158 333 0 0 100 0 0 0 0 0 479424 2216 689364 0 0 0 0 174 342 0 1 99 0 0 0 0 0 479424 2216 689364 0 0 0 0 156 320 0 2 98 0 0 0 0 0 479424 2216 689364 0 0 0 0 174 341 0 0 100 0 0
vmstat
コマンドの実行結果に「時刻」を付けて表示させる場合、以下のコマンドを実行します。
vmstat 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 }'
[root@centos8 ~]# vmstat 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 }' 2021/09/23 02:01:32 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- 2021/09/23 02:01:32 r b swpd free buff cache si so bi bo in cs us sy id wa st 2021/09/23 02:01:32 3 0 0 478964 2216 689424 0 0 67 13 177 346 1 1 98 1 0 2021/09/23 02:01:33 0 0 0 478844 2216 689424 0 0 0 0 187 372 0 1 99 0 0 2021/09/23 02:01:34 0 0 0 478844 2216 689424 0 0 0 0 191 370 0 0 100 0 0 2021/09/23 02:01:35 0 0 0 478844 2216 689424 0 0 0 0 196 364 1 1 98 0 0 2021/09/23 02:01:36 1 0 0 478844 2216 689424 0 0 0 0 179 369 0 1 99 0 0 2021/09/23 02:01:37 0 0 0 478844 2216 689424 0 0 0 0 185 373 0 3 97 0 0 2021/09/23 02:01:38 0 0 0 478844 2216 689424 0 0 0 0 187 372 0 0 100 0 0 2021/09/23 02:01:39 0 0 0 478844 2216 689424 0 0 0 0 187 370 0 1 99 0 0 2021/09/23 02:01:40 0 0 0 478844 2216 689424 0 0 0 0 187 375 0 1 99 0 0 2021/09/23 02:01:41 0 0 0 478844 2216 689424 0 0 0 17 196 377 0 1 99 0 0
ファイル(ここでは「vmstat.log」)に出力する場合、以下のコマンドを実行します。
パイプをリフレッシュするためにsystem(":")
を使用します。
vmstat 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { system(":") }' > vmstat.log
[root@centos8 ~]# vmstat 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { system(":") }' > vmstat.log
[root@centos8 ~]# cat vmstat.log 2021/09/23 02:06:53 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- 2021/09/23 02:06:53 r b swpd free buff cache si so bi bo in cs us sy id wa st 2021/09/23 02:06:53 2 0 0 478796 2216 689456 0 0 65 12 177 346 1 1 98 1 0 2021/09/23 02:06:54 1 0 0 478436 2216 689456 0 0 0 0 197 355 1 2 97 0 0 2021/09/23 02:06:55 0 0 0 478436 2216 689456 0 0 0 0 164 329 0 1 99 0 0 2021/09/23 02:06:56 1 0 0 478436 2216 689456 0 0 0 4 182 346 1 1 98 0 0 2021/09/23 02:06:57 0 0 0 478436 2216 689456 0 0 0 0 165 338 0 1 99 0 0 2021/09/23 02:06:58 0 0 0 478436 2216 689456 0 0 0 0 169 327 0 2 98 0 0 [root@centos8 ~]#
長時間、実行結果を取得する場合、バックグラウンドで動かして、ログアウトしても終了しないようにしたいこともあります。
その場合、nohup
を使用して以下のコマンドを実行します。
nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat.log 2>&1 &
[root@centos8 ~]# nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat.log 2>&1 & [1] 3872 [root@centos8 ~]#
出力ファイル名に時刻を持たせたい場合、以下のコマンドを実行します。
nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat_$(date "+%Y%m%d_%H%M%S").log 2>&1 &
[root@centos8 log]# nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat_$(date "+%Y%m%d_%H%M%S").log 2>&1 & [1] 3931 [root@centos8 log]# ls vmstat_20210923_021542.log [root@centos8 log]# cat vmstat_20210923_021542.log nohup: 入力を無視します 21/09/23 02:15:42 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- 21/09/23 02:15:42 r b swpd free buff cache si so bi bo in cs us sy id wa st 21/09/23 02:15:42 2 0 0 478404 2216 689688 0 0 61 12 177 346 1 1 98 1 0 21/09/23 02:15:43 0 0 0 477684 2216 689692 0 0 0 0 221 381 1 1 98 0 0 21/09/23 02:15:44 1 0 0 477684 2216 689692 0 0 0 0 208 402 1 2 97 0 0 21/09/23 02:15:45 0 0 0 477684 2216 689692 0 0 0 0 189 370 0 1 99 0 0 21/09/23 02:15:46 0 0 0 477684 2216 689692 0 0 0 0 199 381 0 1 99 0 0 21/09/23 02:15:47 0 0 0 477684 2216 689692 0 0 0 5 208 395 0 1 99 0 0 21/09/23 02:15:48 1 0 0 477684 2216 689692 0 0 0 0 214 401 1 3 96 0 0 21/09/23 02:15:49 0 0 0 477684 2216 689692 0 0 0 0 219 390 1 1 98 0 0 21/09/23 02:15:50 0 0 0 477684 2216 689692 0 0 0 0 225 417 0 2 98 0 0 21/09/23 02:15:51 0 0 0 477684 2216 689692 0 0 0 0 210 390 1 1 98 0 0 [root@centos8 log]#
参考
【 vmstat 】コマンド――仮想メモリやディスクI/Oの統計情報を表示する:Linux基本コマンドTips(126) - @IT
--
以上