我正在开发一个嵌入式 Linux 系统(kernel-5.24.0),其中有 128MB RAM。
现在我发现一个进程存在内存泄漏(来自free
和available
的输出free -k
),我试图找出泄漏来自哪里。
所以我使用了著名的valgrind --leak-check=yes --tool=memcheck myapp
,运行9个小时后我得到了以下总结,
==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
我发现free
和Available
正在减少(减少的计数超过 318,088 字节),所以我很困惑。
我pmap -x pidof myapp
发现进程的PSS
和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
,PSS
和Dirty
缓慢增加,有没有办法(用户空间工具或内核功能)来帮助跟踪内存请求来自哪里myapp
?