はじめに
システムのメモリ使用状況を監視したい時がある。top
や htop
も便利だが、もう少し詳細にメモリの使用状況を見たい場合がある。
smemstat
というメモリ統計監視ツールがあったので使ってみる。
このツールはカーネルレベルでのメモリ使用状況を詳細に監視できる。
環境
Windows 11 Professional
WSL2 - (Ubuntu 24.04 LTS)
smemstat 0.02.12
smemstatのインストール
sudo apt install smemstat
ログ
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
crack-common libdouble-conversion3 libgl1-amber-dri libglapi-mesa libllvm17t64 libmd4c0 libpcre2-16-0
libqt5core5t64 libqt5dbus5t64 libqt5gui5t64 libqt5network5t64 libqt5qml5 libqt5qmlmodels5 libqt5quick5
libqt5svg5 libqt5waylandclient5 libqt5waylandcompositor5 libqt5widgets5t64 libqt5x11extras5
libxcb-damage0 libxcb-xinerama0 libxcb-xinput0 mesa-utils-bin qt5-gtk-platformtheme qttranslations5-l10n
qtwayland5 xbitmaps
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
smemstat
0 upgraded, 1 newly installed, 0 to remove and 14 not upgraded.
Need to get 17.1 kB of archives.
After this operation, 54.3 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble/universe amd64 smemstat amd64 0.02.13-1 [17.1 kB]
Fetched 17.1 kB in 1s (22.7 kB/s)
Selecting previously unselected package smemstat.
(Reading database ... 128143 files and directories currently installed.)
Preparing to unpack .../smemstat_0.02.13-1_amd64.deb ...
Unpacking smemstat (0.02.13-1) ...
Setting up smemstat (0.02.13-1) ...
Processing triggers for man-db (2.12.0-4build2) ...
バージョンを確認する。
※ バージョンは -h
でhelp
と一緒に出てくる。
smemstat -h
smemstat, version 0.02.13
smemstatを使う
まずは help
を見てみる。
smemstat -h
Usage: smemstat [options] [duration] [count]
Options are:
-a show memory change with up/down arrows
-c get command name from processes comm field
-d strip directory basename off command information
-g report memory in gigabytes
-h show this help information
-k report memory in kilobytes
-l show long (full) command information
-m report memory in megabytes
-o file dump data to json formatted file
-p proclist specify comma separated list of processes to monitor
-q run quietly, useful for -o output only
-s show short command information
-t top mode, show only changes in memory
-T top mode, show top memory hoggers
smemstatのオプション一覧
オプション | 説明 |
---|---|
-a | メモリ変化を上向き/下向き矢印で表示する |
-c | プロセスのcommフィールドからコマンド名を取得する |
-d | コマンド情報からディレクトリのベース名を除去する |
-g | メモリをギガバイト単位で表示する |
-h | ヘルプ情報を表示する |
-k | メモリをキロバイト単位で表示する |
-l | 長い(完全な)コマンド情報を表示する |
-m | メモリをメガバイト単位で表示する |
-o file | データをJSON形式のファイルにダンプする |
-p proclist | 監視するプロセスのカンマ区切りリストを指定する |
-q | 静寂モードで実行(-o での出力のみに便利) |
-s | 短いコマンド情報を表示する |
-t | トップモード、メモリの変化のみを表示する |
-T | トップモード、メモリを最も多く使用するプロセスを表示する |
基本的な使い方
何もオプションをつけずに実行してみる。5秒間隔で監視を開始する。
smemstat 5
PID Swap USS PSS RSS User Command
6260 0.0 B 208.8 K 164.6 K 0.0 B kbushi /usr/bin/zsh
6494 0.0 B 0.0 B 59.4 K 0.0 B kbushi /usr/bin/zsh
6495 0.0 B 0.0 B 59.0 K 0.0 B kbushi /usr/bin/zsh
6407 0.0 B 0.0 B 17.0 K 0.0 B kbushi /usr/bin/zsh
19868 0.0 B 4096.0 B 4096.0 B 4096.0 B kbushi smemstat
1836 0.0 B 819.2 B 1638.4 B 819.2 B kbushi /home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node
1611 0.0 B 819.2 B 1638.4 B 819.2 B kbushi /home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node
2129 0.0 B 0.0 B 819.2 B 0.0 B kbushi /home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node
1620 0.0 B 0.0 B 819.2 B 0.0 B kbushi /home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node
1598 0.0 B 0.0 B 819.2 B 0.0 B kbushi /home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node
688 0.0 B 0.0 B 819.2 B 0.0 B kbushi zsh (qterm)
...
各列の意味
列名 | 説明 |
---|---|
PID | プロセスID |
Process | プロセス名 |
Swap | スワップ使用量 |
USS | Unique Set Size(そのプロセス固有のメモリ使用量) |
PSS | Proportional Set Size(共有メモリを按分したメモリ使用量) |
RSS | Resident Set Size(物理メモリ上の使用量) |
User | プロセスの実行ユーザー |
特定のプロセスを監視
-p
オプションで特定のプロセスIDを監視する。
# まずプロセスIDを確認
pgrep docker
1363
3572
3592
3604
3618
3636
3655
3754
smemstat -p 1363,3572,3592,3604,3618,3636,3655,3754
PID Swap USS PSS RSS User Command
3592 0.0 B 10.2 M 12.8 M 28.4 M kbushi docker
3604 0.0 B 10.2 M 12.8 M 28.3 M kbushi docker
3618 0.0 B 10.1 M 12.8 M 28.4 M kbushi docker
3636 0.0 B 10.2 M 12.8 M 28.3 M kbushi docker
3754 0.0 B 10.0 M 12.7 M 28.3 M kbushi docker
3572 0.0 B 8376.0 K 10.8 M 26.4 M kbushi docker
3655 0.0 B 7956.0 K 10.4 M 25.9 M kbushi docker
Total 0.0 B 66.7 M 85.1 M 193.9 M
特定のプロセスのみの監視ができる。
変化のみ表示
-t
オプションで変化のみを表示する
smemstat -t
JSONファイルに出力
-o
オプションで結果をJSONファイルに保存する。
smemstat -o test.json
cat test.json
{
"smemstat":{
"smem-per-process":[
{
"pid":1824,
"user":"kbushi",
"command":"/home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node",
"swap":0,
"uss":753287168,
"pss":763802624,
"rss":814071808
},
{
"pid":1598,
"user":"kbushi",
"command":"/home/kbushi/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/node",
"swap":0,
"uss":86351872,
"pss":94736384,
"rss":141160448
},
...
JSONファイルで保存されるので、後から分析できる。
短縮形フィールド名で表示
-s
オプションで短縮形のフィールド名を使用する。
smemstat -s
ターミナルの幅が狭い場合に便利。
参考
パッケージ: smemstat (0.02.13-1) | Ubuntu packages
https://packages.ubuntu.com/noble/smemstatColinIanKing / smemstat
https://github.com/ColinIanKing/smemstat
おわりに
smemstat
は詳細なメモリ監視ができるツールとなっている。top
や htop
では見えない USS、PSS といった詳細なメモリ指標が確認できるのが良い。
メモリリークやどのプロセスがメモリ使用しているのかを詳細に確認できるので今後使っていきたい。