nazo6 notememo

Rustアプリのメモリ使用量を調査する

作成:2025/03/29

更新:2025/03/29

方法1. nmコマンド

nm ./target/release/bin_name -C -S -n # アドレス順
nm ./target/release/bin_name -C -S --size-sort # サイズ順
主にstatic領域のメモリ使用量を調査するのに有益。embassyの独立したタスクなどのサイズが見れる。

方法2. elf-size-analyze

uvx elf-size-analyze -HaR ./target/release/bin_name -w 200 -m 200
元は型ごとにバイナリサイズを表示できるコマンド(-HaF)だが、-HaRオプションを付けることで静的メモリサイズの解析もできる。

方法3. -Zprint-type-sizes

nightly限定のrustcで使えるフラグである-Zprint-type-sizesを使う。これはコンパイル時に全ての型のサイズを出力してくれるものである。async関数などでfutureが想定よりデカい可能性があるときなどに使える。
この出力をtop-type-sizesというツールに通せば見やすくしてくれる。
RUSTFLAGS=-Zprint-type-sizes cargo build --release -j 1 > sizes.txt
top-type-sizes < sizes.txt | less
nushellの例:
RUSTFLAGS=-Zprint-type-sizes cargo build --release -j 1 --features right out> size.txt
open size.txt | top-type-sizes -w -r -l 100