今天小試了一把valgrind的massif工具。Massif在valgrind中的介紹是這樣的:
Massif
Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.
可以看出Massif是一個內存剖析工具。通過不斷的取程序堆的快照來達到監視程序內存分配的目的。
我們來編寫一個這樣的C++文件:
#include <stdlib.h>
#include <stdio.h>
int *fa()
{
int *p=(int*)malloc(10000);
return p;
}
int *fb(int *p)
{
delete p;
}
int main(void)
{
printf("ok\n");
printf("really ok?\n");
int *vec[10000];
for(int i=0;i<10000;i++)
{
vec[i]=fa();
}
for(int i=0;i<10000;i++)
{
fb(vec[i]);
}
return 0;
}
使用g++進行編譯:
g++ test.cc -o test
然後使用valgrind的massif:
valgrind --tool=massif ./test
最後就得到一個massif文件:massif.out.32682 (32682代碼進程pid)
使用ms_print來解析這個輸出文件:
ms_print massif.out.32682
最後得到解析結果:
通過圖形可以看出Heap隨時間變化的變化,可以通過分析下面的函數信息得到究竟是那些函數佔用了大量的內存。
原文鏈接:http://jdoc.iteye.com/admin/blogs/1217804