mtrace监测程序内存泄露

本文介绍了如何使用mtrace工具来查找程序中的内存泄漏问题。通过在main函数开头调用mtrace(),编译时添加-g选项,设置MALLOC_TRACE环境变量,运行程序并使用mtrace解析输出,可以有效定位内存未释放的问题。
摘要由CSDN通过智能技术生成

 

Finding Memory Leaks Using mtrace

When testing code on small samples, small problems such as memory leaks are often undetectable. However, once the program is run on a larger scale, existing memory leaks become larger problems; they often will cause a program to crash or to swap extensively (slowing performance drastically), and also affect any other programs running on the same system by reducing available memory.

To help avoid, or more often fix, such memory leaks, there are many tools available. One within the GNU platform is called mtrace.

Steps to Profiling a Program

  1. Add a call to mtrace() at the very start of your main function, and include mcheck.h in the file containing the main program.
  2. Compile the program with debug options set (i.e. the '-g' switch)
  3. export a variable called MALLOC_TRACE which contains a filename where the trace data should be stored. Example: export MALLOC_TRACE=mtrace.out
  4. Run the program, once
  5. Run 'mtrace my_program mtrace.out' to view the output

1. Add the mtrace() call

mtrace works by adding hooks into calls which allocate and deallocate memory, logging these calls, and reporting on any mis-matches between the recorded allocations and deallocations. In order t

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值