Linuxでプロセスのメモリリークをキャッチする方法は?

Linuxでプロセスのメモリリークをキャッチする方法は?

私は128MBのRAMを備えた組み込みLinuxシステム(kernel-5.24.0)を開発しています。

今、私はプロセスにメモリリーク(freeおよびavailable出力free -k)があることを発見し、リークがどこで発生するのかを調べようとしています。

それで、有名なものを使ってvalgrind --leak-check=yes --tool=memcheck myapp9時間実行した後、次の要約が得られました。

==6573== HEAP SUMMARY:
==6573==     in use at exit: 34,793,251 bytes in 317,231 blocks
==6573==   total heap usage: 50,635,131 allocs, 50,317,900 frees, 2,733,852,406 bytes allocated
....
==6573== LEAK SUMMARY:
==6573==    definitely lost: 32 bytes in 2 blocks
==6573==    indirectly lost: 0 bytes in 0 blocks
==6573==      possibly lost: 318,088 bytes in 5,321 blocks
==6573==    still reachable: 34,475,131 bytes in 311,908 blocks
==6573==         suppressed: 0 bytes in 0 blocks
==6573== Reachable blocks (those to which a pointer was found) are not shown.
==6573== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==6573==
==6573== Use --track-origins=yes to see where uninitialised values come from
==6573== For lists of detected and suppressed errors, rerun with: -s
==6573== ERROR SUMMARY: 1893697 errors from 477 contexts (suppressed: 0 from 0)

と報告されるのにpossibly lost: 318,088 bytes合計が減少するfree -kのを見て(減少する個数が318,088バイトを超える)混乱します。freeAvailable

pmap -x pidof myappプロセスPSS合計(KB単位)がゆっくり増加していることがわかりましたDirty[heap]

XXXXXXXXXXXXXXXXXXXXXXXXx 294
              total        used        free      shared  buff/cache   available
Mem:         117128       41792       49996        7956       25340       64772
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5060    5012    5012       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

XXXXXXXXXXXXXXXXXXXXXXXXx 295
              total        used        free      shared  buff/cache   available
Mem:         117128       41848       49940        7956       25340       64716
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5060    5012    5012       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]
........
........

XXXXXXXXXXXXXXXXXXXXXXXXx 583
              total        used        free      shared  buff/cache   available
Mem:         117128       41940       49848        7956       25340       64624
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5092    5092       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

XXXXXXXXXXXXXXXXXXXXXXXXx 584
              total        used        free      shared  buff/cache   available
Mem:         117128       41896       49892        7956       25340       64668
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5092    5092       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

......
XXXXXXXXXXXXXXXXXXXXXXXXx 759
              total        used        free      shared  buff/cache   available
Mem:         117128       42028       49760        7956       25340       64536
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5092    5092       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

XXXXXXXXXXXXXXXXXXXXXXXXx 760
              total        used        free      shared  buff/cache   available
Mem:         117128       41864       49924        7956       25340       64700
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5092    5092       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

.......
.......
XXXXXXXXXXXXXXXXXXXXXXXXx 1000
              total        used        free      shared  buff/cache   available
Mem:         117128       41756       50032        7956       25340       64804
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5092    5092       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

XXXXXXXXXXXXXXXXXXXXXXXXx 1001
              total        used        free      shared  buff/cache   available
Mem:         117128       41896       49892        7956       25340       64664
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5116    5116       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

XXXXXXXXXXXXXXXXXXXXXXXXx 1002
              total        used        free      shared  buff/cache   available
Mem:         117128       41796       49992        7956       25340       64764
Swap:             0           0           0

1307: myapp
Address           Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000       4       4       0       0  r-xp  /usr/bin/myapp
0000000000410000       4       4       4       0  rwxp  /usr/bin/myapp
0000000000c06000    5192    5116    5116       0  rwxp  [heap]
000000007009f000    2208    2208    2208       0  rwxp    [ anon ]
00000000703ad000     936     936     936       0  rwxp    [ anon ]
0000000070497000       4       0       0       0  ---p    [ anon ]
0000000070498000    8192       8       8       0  rwxp    [ anon ]
0000000070c98000       4       0       0       0  ---p    [ anon ]

私の場合、Valgrindのレポートは正しいですか?possibly lost理解でき、修正する価値がありますか?プロセスのメモリ使用量がmyapp増加する傾向です(量はキロバイト単位で変動しますが、傾向は増加しています)。メモリリークを識別するのに役立つ他の方法(またはvalgrindのツール)がありますか?

出力パターンとゆっくりとした増加で判断pmapすると、メモリ要求がどこから来るかを追跡するのに役立つ方法(ユーザースペースツールまたはカーネル機能)がありますか?PSSDirtymyapp

ベストアンサー1

おすすめ記事