关于linux:valgrind、massif、top、pmap的比较
Comparing valgrind, massif, top, and pmap
我正在尝试了解我的应用程序是否泄漏。
在运行我的应用程序时,我会定期运行 pmap 并观察:
1 2 3 4 |
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 |
我运行 top 并观察:
1 2 3 4 5 6 7 |
VIRT RES SHR 488260 17684 3020 488256 20060 3032 488256 22700 3032 488256 26132 3032 488256 28772 3032 488256 31880 3032 |
"RES"和"可写/私有"的增加让我怀疑存在泄漏。但是,运行 valgrind 我没有检测到任何重大泄漏,并且当我中止执行时,我始终如一
看到大约 20Mb 可达内存:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
==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) |
我运行 valgrind --tool=massif 并且还看到 20Mb flat:

问题:有人能解释一下为什么 valgrind 和 massif 告诉我我的程序一直使用 20Mb 内存,但 top 和 pmap 告诉我使用量在增长吗?
相关讨论
- 为了理解为什么 top 显示增加,您还需要使用 valgrind --pages-as-heap=yes 分析程序中的内存分配。你会明白为什么会增加。这是 top 测量进程内存消耗的方式。 valgrind.org/docs/manual/ms-manual.html#ms-manual.not-measur??ed。并且使用 valgrind --pages-as-heap=yes 您将看到这些分配在您的程序中完成的位置。
- 谢谢哥们,太棒了至少现在我可以让plot显示增加。现在我只需要弄清楚如何在我的代码中追踪它。
为了理解为什么你的进程的top显示增加,你还需要用valgrind --pages-as-heap=yes分析你的程序中的内存分配。你会明白为什么会增加。这是 top 测量进程内存消耗的方式。 http://valgrind.org/docs/manual/ms-manual.html#ms-manual.not-measured。并且使用 valgrind --pages-as-heap=yes 您将看到这些分配在您的程序中完成的位置