一、前言
       对于C/C++程序员来说,关于内存问题真是让人头痛不已,尤其是内存泄露、使用未初始化的局部变量进行跳转或移动等隐形问题。要求程序员养成良好的编码习惯确实很重要,但是人总会出现稀里糊涂的时候,遇到内存泄露等问题还是在所难免,好在有了众多的内存检测工具,真是帮了程序员的大忙啊。下面将介绍一款强大的Linux开源工具Valgrind,非常棒,内存检测工具中的瑞士军刀。

二、简介
       Valgrind 是一款 Linux下C/C++程序的内存检测工具,一个显著的特点是无需重新编译,可以直接对debug版本的程序进行分析测试。
       目前,最新的版本valgrind-3.8.1支持X86/Linux, AMD64/Linux, ARM/Linux, PPC32/Linux, PPC64/Linux,
S390X/Linux, MIPS/Linux, ARM/Android (2.3.x and later), X86/Android (4.0 and later), X86/Darwin and AMD64/Darwin (Mac OS X 10.6 and 10.7, with limited support for 10.8)。
       相关资料:
1、http://valgrind.org/ (官网,资料全面,很详实,但全是英文,全力推荐)
2、http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/ (IBM的中文资料,很详细,全力推荐)
3、http://blog.csdn.net/yanghao23/article/details/7514587  (网友的总结体会,写得好)

三、快速入门
1、下载与安装
上官网下载最新版本:valgrind-3.8.1,按如下步骤在解压并进行默认安装:
tar xvf valgrind-3.8.1.tar.bz2
cd valgrind-3.8.1/
./configure
make
make install


2、快速使用(对Linux系统的程序“ls”进行检测)
valgrind ls /    (快速进行默认Memcheck检测)
valgrind --leak-check=full --track-origins=yes -v ls /    (进行Memcheck检测,并打印详细的结果,不过速度慢了很多,这个我用得更多)

root@root:/media/workspace/# valgrind ls /

==20361== Memcheck, a memory error detector

==20361== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.

==20361== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==20361== Command: ls /

==20361==

bin   cdrom  dev  gnome-terminal.desktop  initrd.img      lib    lib64       media  opt   root  selinux  swap  tftpboot      tmp  var      vmlinuz.old

boot  data   etc  home                    initrd.img.old  lib32  lost+found  mnt    proc  sbin  srv      sys   tftpboot.bak  usr  vmlinuz

==20361==

==20361== HEAP SUMMARY:

==20361==     in use at exit: 33,197 bytes in 38 blocks

==20361==   total heap usage: 567 allocs, 529 frees, 97,898 bytes allocated

==20361==

==20361== LEAK SUMMARY:

==20361==    definitely lost: 120 bytes in 1 blocks   【哎呦,居然有内存泄露,奇怪,系统的问题,咱管不了它的代码,skip it】

==20361==    indirectly lost: 0 bytes in 0 blocks

==20361==      possibly lost: 0 bytes in 0 blocks

==20361==    still reachable: 33,077 bytes in 37 blocks

==20361==         suppressed: 0 bytes in 0 blocks

==20361== Rerun with --leak-check=full to see details of leaked memory

==20361==

==20361== For counts of detected and suppressed errors, rerun with: -v

==20361== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)   【哦哦,还好,没有其它错误】

root@root:/media/workspace/# 

root@root:/media/workspace# valgrind --leak-check=full --track-origins=yes -v
ls /

==21828== Memcheck, a memory error detector

==21828== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.

==21828== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info

==21828== Command: ls /

==21828==

--21828-- Valgrind options:

--21828--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp

--21828--    --leak-check=full

--21828--    --track-origins=yes

--21828--    -v

--21828-- Contents of /proc/version:

--21828--   Linux version 2.6.32-45-generic (buildd@batsu) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) ) #101-Ubuntu SMP Mon Dec 3 15:39:38 UTC 2012

--21828-- Arch and hwcaps: AMD64, amd64-sse3-cx16

--21828-- Page sizes: currently 4096, max supported 4096

--21828-- Valgrind library directory: /usr/lib/valgrind

--21828-- Reading syms from /bin/ls (0x400000)

--21828-- Reading debug info from /bin/ls ..

--21828-- .. CRC mismatch (computed d5468613 wanted 88b6ebd9)

--21828--    object doesn't have a symbol table

--21828-- Reading syms from /lib/ld-2.11.1.so (0x4000000)

--21828-- Reading debug info from /lib/ld-2.11.1.so ..

--21828-- .. CRC mismatch (computed f7ae097b wanted a9b86b80)

--21828-- Reading debug info from /usr/lib/debug/lib/ld-2.11.1.so ..

--21828-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux (0x38000000)

--21828--    object doesn't have a dynamic symbol table

--21828-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp

--21828-- Reading suppressions file: /usr/lib/valgrind/default.supp

--21828-- REDIR: 0x40183b0 (strlen) redirected to 0x380402d7 (vgPlain_amd64_linux_REDIR_FOR_strlen)

--21828-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so (0x4a22000)

--21828-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so (0x4c24000)

==21828== WARNING: new redirection conflicts with existing -- ignoring it

--21828--     new: 0x040183b0 (strlen              ) R-> 0x04c28710 strlen

--21828-- REDIR: 0x4018220 (index) redirected to 0x4c28320 (index)

--21828-- REDIR: 0x40182a0 (strcmp) redirected to 0x4c28cf0 (strcmp)

--21828-- Reading syms from /lib/librt-2.11.1.so (0x4e2d000)

--21828-- Reading debug info from /lib/librt-2.11.1.so ..

--21828-- .. CRC mismatch (computed 8fbc67b2 wanted c6419430)

--21828-- Reading debug info from /usr/lib/debug/lib/librt-2.11.1.so ..

--21828-- Reading syms from /lib/libselinux.so.1 (0x5035000)

--21828--    object doesn't have a symbol table

--21828-- Reading syms from /lib/libacl.so.1.1.0 (0x5253000)

--21828-- Reading debug info from /lib/libacl.so.1.1.0 ..

--21828-- .. CRC mismatch (computed c1ea3c04 wanted 297d6d26)

--21828--    object doesn't have a symbol table

--21828-- Reading syms from /lib/libc-2.11.1.so (0x545b000)

--21828-- Reading debug info from /lib/libc-2.11.1.so ..

--21828-- .. CRC mismatch (computed 6b23738b wanted c0dae497)

--21828-- Reading debug info from /usr/lib/debug/lib/libc-2.11.1.so ..

--21828-- Reading syms from /lib/libpthread-2.11.1.so (0x57e1000)

--21828-- Reading debug info from /lib/libpthread-2.11.1.so ..

--21828-- .. CRC mismatch (computed d460a184 wanted 81aea168)

--21828-- Reading debug info from /usr/lib/debug/lib/libpthread-2.11.1.so ..

--21828-- Reading syms from /lib/libdl-2.11.1.so (0x59fe000)

--21828-- Reading debug info from /lib/libdl-2.11.1.so ..

--21828-- .. CRC mismatch (computed 06532688 wanted 2f6eef4b)

--21828-- Reading debug info from /usr/lib/debug/lib/libdl-2.11.1.so ..

--21828-- Reading syms from /lib/libattr.so.1.1.0 (0x5c02000)

--21828-- Reading debug info from /lib/libattr.so.1.1.0 ..

--21828-- .. CRC mismatch (computed aa603317 wanted 08b26063)

--21828--    object doesn't have a symbol table

--21828-- REDIR: 0x54e1100 (__GI_strrchr) redirected to 0x4c28140 (__GI_strrchr)

--21828-- REDIR: 0x54d8b20 (malloc) redirected to 0x4c27426 (malloc)

--21828-- REDIR: 0x54e1810 (memchr) redirected to 0x4c28d90 (memchr)

--21828-- REDIR: 0x54e3290 (memcpy) redirected to 0x4c28dc0 (memcpy)

--21828-- REDIR: 0x54ddb50 (__GI_strchr) redirected to 0x4c28220 (__GI_strchr)

--21828-- REDIR: 0x54da410 (free) redirected to 0x4c27036 (free)

--21828-- REDIR: 0x54e10d0 (rindex) redirected to 0x4a225dc (_vgnU_ifunc_wrapper)

==21828== WARNING: new redirection conflicts with existing -- ignoring it

--21828--     new: 0x054e1100 (__GI_strrchr        ) R-> 0x04c28110 rindex

--21828-- REDIR: 0x54ddc10 (__GI_strcmp) redirected to 0x4c28ca0 (__GI_strcmp)

--21828-- REDIR: 0x54df640 (__GI_strlen) redirected to 0x4c286d0 (__GI_strlen)

--21828-- REDIR: 0x54df850 (__GI_strncmp) redirected to 0x4c28be0 (__GI_strncmp)

--21828-- REDIR: 0x54e4870 (strchrnul) redirected to 0x4c29a10 (strchrnul)

--21828-- REDIR: 0x54e29b0 (mempcpy) redirected to 0x4c29a80 (mempcpy)

--21828-- REDIR: 0x54da5c0 (realloc) redirected to 0x4c274d7 (realloc)

--21828-- REDIR: 0x54df690 (strnlen) redirected to 0x4c28630 (strnlen)

--21828-- REDIR: 0x54e2ff0 (__GI_stpcpy) redirected to 0x4c296c0 (__GI_stpcpy)

--21828-- REDIR: 0x54df090 (__GI_strcpy) redirected to 0x4c28800 (__GI_strcpy)

--21828-- REDIR: 0x54e4820 (__GI___rawmemchr) redirected to 0x4c29a60 (__GI___rawmemchr)

--21828-- REDIR: 0x54df610 (strlen) redirected to 0x4a225dc (_vgnU_ifunc_wrapper)

==21828== WARNING: new redirection conflicts with existing -- ignoring it

--21828--     new: 0x054df640 (__GI_strlen         ) R-> 0x04c286b0 strlen

--21828-- REDIR: 0x54e1cc0 (memmove) redirected to 0x4c299a0 (memmove)

bin   cdrom  dev  gnome-terminal.desktop  initrd.img      lib    lib64       media  opt   root  selinux  swap  tftpboot      tmp  var      vmlinuz.old

boot  data   etc  home                    initrd.img.old  lib32  lost+found  mnt    proc  sbin  srv      sys   tftpboot.bak  usr  vmlinuz

==21828==

==21828== HEAP SUMMARY:

==21828==     in use at exit: 33,197 bytes in 38 blocks

==21828==   total heap usage: 567 allocs, 529 frees, 97,898 bytes allocated

==21828==

==21828== Searching for pointers to 38 not-freed blocks

==21828== Checked 144,976 bytes

==21828==

==21828== 120 bytes in 1 blocks are definitely lost in loss record 3 of 8

==21828==    at 0x4C274A8: malloc (vg_replace_malloc.c:236)

==21828==    by 0x54C5748: getdelim (iogetdelim.c:68)

==21828==    by 0x5041EB2: ??? (in /lib/libselinux.so.1)

==21828==    by 0x504A465: ??? (in /lib/libselinux.so.1)

==21828==    by 0x503A2D2: ??? (in /lib/libselinux.so.1)

==21828==    by 0x7FF0007CD: ???

==21828==    by 0x207C3D4E45504F52: ???

==21828==    by 0x6E69622F7273752E: ???

==21828==    by 0x7069707373656C2E: ???

==21828==    by 0x4E414C0073252064: ???

==21828==    by 0x687A3D4547415546: ???

==21828==    by 0x5500687A3A4E435E: ???

==21828==

==21828== LEAK SUMMARY:

==21828==    definitely lost: 120 bytes in 1 blocks

==21828==    indirectly lost: 0 bytes in 0 blocks

==21828==      possibly lost: 0 bytes in 0 blocks

==21828==    still reachable: 33,077 bytes in 37 blocks

==21828==         suppressed: 0 bytes in 0 blocks

==21828== Reachable blocks (those to which a pointer was found) are not shown.

==21828== To see them, rerun with: --leak-check=full --show-reachable=yes

==21828==

==21828== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

--21828--

--21828-- used_suppression:      2 dl-hack3-cond-1

--21828-- used_suppression:      2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a

==21828==

==21828== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

root@root:/media/workspace# 



Valgrind的Memcheck快速入门的更多相关文章

  1. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  2. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  3. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  4. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  5. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. Mybatis框架 的快速入门

    MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...

  7. grunt快速入门

    快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...

  8. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  9. 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. 20145307第七周JAVA学习报告

    20145307<Java程序设计>第七周学习总结 教材学习内容总结 Lambda Lambda语法概述: Arrays的sort()方法可以用来排序,在使用sort()时,需要操作jav ...

  2. COGS 293.[NOI2000] 单词查找树

    ★   输入文件:trie.in   输出文件:trie.out   简单对比 时间限制:1 s   内存限制:128 MB 在进行文法分析的时候,通常需要检测一个单词是否在我们的单词列表里.为了提高 ...

  3. git如何生成单个文件的补丁

    背景:有时候碰到一个commit包含了好几个文件的修改,但是我只需要其中一个文件的修改内容,那么这时候就需要以下方法来生成这一个文件对应修改内容的补丁 答:git format-patch " ...

  4. ubuntu18.04编译openwrt前的准备

    1.获取openwrt源码 git clone https://github.com/openwrt/openwrt.git 2.安装一些库及必备程序: sudo apt-get install li ...

  5. unix_timestamp() 和 from_unixtime()

    unix_timestamp() 将时间转换为时间戳.(date 类型数据转换成 timestamp 形式整数) select unix_timestamp('2016-03-23 11:10:10' ...

  6. CentOS7配置自定义JDK

    由于选择的是centos7-dvd镜像自带了open jdk,需要卸载.首先查询自带的jdk: rpm -qa | grep java 会出现7个带有java名称的东西,删除其中的带有openjdk字 ...

  7. centos cgroup配置

    centOS 6:1. 启用cgroup    查看内核是否支持cgroup功能:cat /boot/config-`uname -r` | grep -i rt_group    查看支持的子系统: ...

  8. 利用Java.util.UUID来生成唯一ID(用来做数据库主键好用)

    UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.按照开放软件基金会(OSF)制定的标准计算, ...

  9. [eclipse]Syntax error on tokens, delete these tokens问题解决

    错误:Syntax error on tokens, delete these tokens 出现这样的错误一般是括号.中英文字符.中英文标点.代码前面的空格,尤其是复制粘贴的代码,去掉即可. 如下图 ...

  10. JavaScript Browser 对象 实例

    使用JavaScript来访问和控制浏览器对象实例. Window 对象 弹出一个警告框 弹出一个带折行的警告框 弹出一个确认框,并提醒访客点击的内容 弹出一个提示框 点击一个按钮时,打开一个新窗口 ...