Search code examples
linuxmemory-managementmemory-leaksvalgrindmassif

Comparing valgrind, massif, top, and pmap


I am trying to understand whether or not my application is leaking.

When running my application, I periodically run pmap and observe:

mapped: 488256K    writeable/private: 90144K    shared: 0K
mapped: 488260K    writeable/private: 101912K    shared: 0K
mapped: 488256K    writeable/private: 102708K    shared: 0K
mapped: 488260K    writeable/private: 105112K    shared: 0K

I run top and observe:

VIRT    RES     SHR
488260  17684   3020
488256  20060   3032
488256  22700   3032
488256  26132   3032
488256  28772   3032
488256  31880   3032

The increase in "RES" and in "writeable/private" is what makes me suspect a leak. However, running valgrind I do not detect any major leak, and when I abort execution I consistently see about 20Mb reachable memory:

==19998== 
==19998== HEAP SUMMARY:
==19998==     in use at exit: 20,351,513 bytes in 974 blocks
==19998==   total heap usage: 329,404 allocs, 328,430 frees, 34,562,346 bytes allocated
==19998== 
==19998== LEAK SUMMARY:
==19998==    definitely lost: 63 bytes in 4 blocks
==19998==    indirectly lost: 0 bytes in 0 blocks
==19998==      possibly lost: 4,679 bytes in 76 blocks
==19998==    still reachable: 20,346,771 bytes in 894 blocks
==19998==         suppressed: 0 bytes in 0 blocks
==19998== Rerun with --leak-check=full to see details of leaked memory
==19998== 
==19998== For counts of detected and suppressed errors, rerun with: -v
==19998== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

I run valgrind --tool=massif and also see 20Mb flat:

massif visualisation

QUESTION: Can someone please explain why valgrind and massif tell me that my program uses 20Mb memory consistently, but top and pmap tell me usage is growing?


Solution

  • In order to understand why top shows increase for your process you also need to analyze memory allocations in your program with valgrind --pages-as-heap=yes. You will understand why there is increase. It is the way top measures memory consumption of a process. http://valgrind.org/docs/manual/ms-manual.html#ms-manual.not-measured. And with valgrind --pages-as-heap=yes you will see where these allocations are done in you program