Valgrind 检测程序内存使用
Valgrind是用于构建动态分析工具的探测框架。它包括一个工具集,每个工具执行某种类型的调试、分析或类似的任务,以帮助完善你的程序。Valgrind的架构是模块化的,所以可以容易地创建新的工具而又不会扰乱现有的结构。
许多有用的工具被作为标准而提供。
- Memcheck是一个内存错误检测器。它有助于使你的程序,尤其是那些用C和C++写的程序,更加准确。
- Cachegrind是一个缓存和分支预测分析器。它有助于使你的程序运行更快。
- Callgrind是一个调用图缓存生成分析器。它与Cachegrind的功能有重叠,但也收集Cachegrind不收集的一些信息。
- Helgrind是一个线程错误检测器。它有助于使你的多线程程序更加准确。
- DRD也是一个线程错误检测器。它和Helgrind相似,但使用不同的分析技术,所以可能找到不同的问题。
- Massif是一个堆分析器。它有助于使你的程序使用更少的内存。
- DHAT是另一种不同的堆分析器。它有助于理解块的生命期、块的使用和布局的低效等问题。
- SGcheck是一个实验工具,用来检测堆和全局数组的溢出。它的功能和Memcheck互补:SGcheck找到Memcheck无法找到的问题,反之亦然。
- BBV是个实验性质的SimPoint基本块矢量生成器。它对于进行计算机架构的研究和开发很有用处。
也有一些对大多数用户没有用的小工具:Lackey是演示仪器基础的示例工具;Nulgrind是一个最小化的Valgrind工具,不做分析或者操作,仅用于测试目的。
在这篇文章我们将关注“memcheck”工具。
在命令行下:
#valgrind --tool=memcheck --leak-check=full ./test
test 为可执行程序名称
编写程序:
int leak_test()
{
char *p = (char *)malloc(10000);
return 0;
}
内存泄漏输出:
==23079== HEAP SUMMARY:
==23079== in use at exit: 10,000 bytes in 1 blocks
==23079== total heap usage: 1 allocs, 0 frees, 10,000 bytes allocated
==23079==
==23079== 10,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
==23079== at 0x4C2DBF6: malloc (vg_replace_malloc.c:299)
==23079== by 0x4005E7: leak_test (in /home/lin/test/memory/test)
==23079== by 0x40068C: main (in /home/lin/test/memory/test)
==23079==
==23079== LEAK SUMMARY:
==23079== definitely lost: 10,000 bytes in 1 blocks
==23079== indirectly lost: 0 bytes in 0 blocks
==23079== possibly lost: 0 bytes in 0 blocks
==23079== still reachable: 0 bytes in 0 blocks
==23079== suppressed: 0 bytes in 0 blocks
==23079==
==23079== For counts of detected and suppressed errors, rerun with: -v
==23079== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
使用非初始化变量:
int uninit_test()
{
int a[5];
int i = 0, s = 0;
//a[0] = a[1] = a[2] = a[3] = a[4] = 0;
a[0] = a[1] = a[2] = a[3] = 0;
for ( i = 0; i < 5; i++)
{
s += a[i];
}
printf("s = %d\r\n", s);
return 0;
}
检测程序输出:
==23175== Conditional jump or move depends on uninitialised value(s)
==23175== at 0x4E87CE2: vfprintf (vfprintf.c:1631)
==23175== by 0x4E8F898: printf (printf.c:33)
==23175== by 0x400663: uninit_test (in /home/lin/test/memory/test)
==23175== by 0x40068C: main (in /home/lin/test/memory/test)
==23175==
s = 4196000
==23175==
==23175== HEAP SUMMARY:
==23175== in use at exit: 0 bytes in 0 blocks
==23175== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==23175==
==23175== All heap blocks were freed -- no leaks are possible
==23175==
==23175== For counts of detected and suppressed errors, rerun with: -v
==23175== Use --track-origins=yes to see where uninitialised values come from
==23175== ERROR SUMMARY: 20 errors from 8 contexts (suppressed: 0 from 0)
[参考文档]
http://www.oschina.net/translate/valgrind-memcheck
http://blog.csdn.net/sduliulun/article/details/7732906
http://blog.csdn.net/destina/article/details/6198443
Valgrind 检测程序内存使用的更多相关文章
- 使用 Valgrind 检测 C++ 内存泄漏
Valgrind 的介绍 Valgrind 可以用来检测程序是否有非法使用内存的问题,例如访问未初始化的内存.访问数组时越界.忘记释放动态内存等问题.在 Linux 可以使用下面的命令安装 Valgr ...
- Unix下C程序内存泄露检测工具:valgrind的安装使用
Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具. Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Goo ...
- c程序内存检测工具 - Valgrind
常用C程序内存泄露检测工具 https://blog.csdn.net/u012662731/article/details/78652651
- C++程序内存泄漏检测方法
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...
- Qt creator 搭配 valgrind 检测内存泄漏
继上次重载operator new检测内存泄漏失败之后,妥协了.决定不管是否是准确指明哪一行代码出现内存泄漏,只要告诉我是否有泄漏就行了,这样就没有new替换的问题.在开发中,总是一个个小功能的开发. ...
- Valgrind检测内存泄露简介
原文地址: Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核 ...
- Linux下利用Valgrind工具进行内存泄露检测和性能分析
from http://www.linuxidc.com/Linux/2012-06/63754.htm Valgrind通常用来成分析程序性能及程序中的内存泄露错误 一 Valgrind工具集简绍 ...
- valgrind--CPP程序内存泄露检查工具
内存泄漏是c++程序常见的问题了,特别是服务类程序,当系统模块过多或者逻辑复杂后,很难通过代码看出内存泄漏. valgrind是一个开源的,检测c++程序内存泄漏有效工具,编译时加上-g选项可以定位到 ...
- 应用 AddressSanitizer 发现程序内存错误
作为 C/ C++ 工程师,在开发过程中会遇到各类问题,最常见便是内存使用问题,比如,越界,泄漏.过去常用的工具是 Valgrind,但使用 Valgrind 最大问题是它会极大地降低程序运行的速度, ...
随机推荐
- Map集合的关联数组实现
public class AssoiativeArray<K,V>{ //创建一个二维数组 private Object[][] pairs; //声明索引 private int ind ...
- xen创建pvm和hvm的过程
these are the basic steps of installing domU with xen-tools in ubuntu13.04 64bit in xen4.3 you can a ...
- OpenSSH/PuTTY/SSH使用
OpenSSH/PuTTY/SSH 常用SSH服务指令 ① 启动SSH服务的命令 service sshd start ② 停止SSH服务的命令 service sshd stop ③ 重新启动SSH ...
- Glide4.0使用
导入 dependencies { compile 'com.github.bumptech.glide:glide:4.0.0' compile 'com.android.support:suppo ...
- 用css画的一个图形 空心正方形+四边四色
div{ width: 100px; height: 100px; border: 100px solid black; border-left-color:darkcyan; border-righ ...
- sqlServer2008技术内幕笔记总结(实用的sql方法总结)
over函数的使用: 1.可以实现基于什么求和,省去group by: select xingming,xingbie,COUNT(*) over() as '总人数' from jbxx_xuesh ...
- ubuntu14.04,安装Git(源代码管理工具)
在shell中执行:sudo apt-get install git-core
- Mybatis注解开发
mybatis 的常用注解: @Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与 @Res ...
- 协程《三》gevent模块
一 gevent模块 安装 pip3 install gevent Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它 ...
- C# Winform 加载窗体/对象时的等待页面设计
在设计应用程序过程中,有时候加载对象需时较长,我们可以显示一个Loading等待页面,对用户来说就比较友好了. 这个还是涉及到多线程,下面是步骤. 一.创建好Loading窗体: 一个Panel用于显 ...