valgrind工具之massif

今天小试了一把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++文件:test.cc

 

 

Cpp代码   收藏代码
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3.   
  4. int *fa()  
  5. {  
  6.   int *p=(int*)malloc(10000);  
  7.   return p;  
  8. }  
  9.   
  10. int *fb(int *p)   
  11. {  
  12.   delete p;  
  13. }  
  14.   
  15. int main(void)  
  16. {  
  17.   printf("ok\n");  
  18.   
  19.   printf("really ok?\n");  
  20.   
  21.   int *vec[10000];  
  22.   
  23.   for(int i=0;i<10000;i++)  
  24.   {  
  25.     vec[i]=fa();  
  26.   }  
  27.   
  28.   
  29.   for(int i=0;i<10000;i++)  
  30.   {  
  31.     fb(vec[i]);  
  32.   }  
  33.   
  34.   return 0;  
  35. }  

使用g++进行编译:

 

Cpp代码   收藏代码
  1. g++ test.cc -o test  

然后使用valgrind的massif:

 

Cpp代码   收藏代码
  1. valgrind --tool=massif ./test  

最后就得到一个massif文件:massif.out.32682 (32682代码进程pid)

使用ms_print来解析这个输出文件:

 

Cpp代码   收藏代码
  1. ms_print massif.out.32682  

 最后得到解析结果:



通过图形可以看出Heap随时间变化的变化,可以通过分析下面的函数信息得到究竟是那些函数占用了大量的内存。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值