저는 128MB RAM을 갖춘 임베디드 Linux 시스템(kernel-5.24.0)을 개발 중입니다.
이제 프로세스에 메모리 누수( 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
것을 보고 (감소하는 개수가 318,088바이트를 초과함) 혼란스럽습니다.free
Available
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
하면 메모리 요청이 어디에서 오는지 추적하는 데 도움이 되는 방법(사용자 공간 도구 또는 커널 기능)이 있습니까 ?PSS
Dirty
myapp