Valgrind

Valgrind作为一个免费且优秀的工具包,平时大部分人可能都是使用valgrind检测内存问题,如内存泄露,越界等。

Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:

Memcheck 

  • 使用未初始化的内存 (Use of uninitialised memory)
  • 使用已经释放了的内存 (Reading/writing memory after it has been free’d)
  • 使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
  • 对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
  • 申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
  • malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
  • src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions

Callgrind

  Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

Cachegrind

  它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。

Helgrind

  它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。

Massif

  堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。

Valgrind使用举例

example.c

 #include<stdio.h>
#include<stdlib.h> int main()
{
int a[]; //4*100=400bytes,stack
int *b = malloc(sizeof(int) * ); //4*100=400bytes,heap 有错误,没有释放内存 return ;
}

安装及使用(Ubuntu16.04)

 /*安装Valgrind*/
sudo apt install valgrind
/*编译源程序*/
gcc example.c -o example /*输入valgrind的有关命令*/
/*默认为tool=memcheck*/
valgrind ./example ==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.11. and LibVEX; rerun with -h for copyright info
==== Command: ./example
====
====
==== HEAP SUMMARY:
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, bytes allocated
====
==== LEAK SUMMARY:
==== definitely lost: bytes in blocks
==== indirectly lost: bytes in blocks
==== possibly lost: bytes in blocks
==== still reachable: bytes in blocks
==== suppressed: bytes in blocks
==== Rerun with --leak-check=full to see details of leaked memory
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from ) /*--leak-check=full可以看内存泄漏的细节*/
valgrind --tool=memcheck --leak-check=full ./example ==3152== Memcheck, a memory error detector
==3152== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==3152== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==3152== Command: ./example
==3152==
==3152==
==3152== HEAP SUMMARY:
==3152== in use at exit: 400 bytes in 1 blocks
==3152== total heap usage: 1 allocs, 0 frees, 400 bytes allocated
==3152==
==3152== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1 //显示了malloc分配的内存泄露
==3152== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3152== by 0x4005B9: main (in /home/rogn/Desktop/MyCode/example)
==3152==
==3152== LEAK SUMMARY:
==3152== definitely lost: 400 bytes in 1 blocks
==3152== indirectly lost: 0 bytes in 0 blocks
==3152== possibly lost: 0 bytes in 0 blocks
==3152== still reachable: 0 bytes in 0 blocks
==3152== suppressed: 0 bytes in 0 blocks
==3152==
==3152== For counts of detected and suppressed errors, rerun with: -v /*上面只能看到堆内存*/
/*使用--tool=massif查看堆栈内存*/
rogn@ubuntu:~/Desktop/MyCode$ valgrind --tool=massif ./example
==== Massif, a heap profiler
==== Copyright (C) -, and GNU GPL'd, by Nicholas Nethercote
==== Using Valgrind-3.11. and LibVEX; rerun with -h for copyright info
==== Command: ./example
====
====
rogn@ubuntu:~/Desktop/MyCode$ ls
example example.c massif.out.
rogn@ubuntu:~/Desktop/MyCode$ ms_print massif.out.
--------------------------------------------------------------------------------
Command: ./example
Massif arguments: (none)
ms_print arguments: massif.out.
-------------------------------------------------------------------------------- B
^ :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
| :
+----------------------------------------------------------------------->ki
102.0 Number of snapshots:
Detailed snapshots: [] --------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
-------------------------------------------------------------------------------- , /*查看整个程序使用的内存和时间*/
rogn@ubuntu:~/Desktop/MyCode$ valgrind --tool=massif --pages-as-heap=yes ./example
==== Massif, a heap profiler
==== Copyright (C) -, and GNU GPL'd, by Nicholas Nethercote
==== Using Valgrind-3.11. and LibVEX; rerun with -h for copyright info
==== Command: ./example
====
====
rogn@ubuntu:~/Desktop/MyCode$ ls //查看当前目录下的文件,发现多了一个名叫massif.out.3191的文件(3191其实就是PID)
example example.c massif.out. massif.out.
rogn@ubuntu:~/Desktop/MyCode$ ms_print massif.out.3191 //使用ms_print程序打印出massif.out.391
--------------------------------------------------------------------------------
Command: ./example
Massif arguments: --pages-as-heap=yes
ms_print arguments: massif.out.
-------------------------------------------------------------------------------- MB
6.172^ //这个就是总的内存了,所谓的Memory limit就是指这个 :
| ::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#:::::::::::::::::::::::::::::
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| : @ # :
| :::: @ # :
| :::: @ # :
| :::: @ # :
| :::: @ # :
| :::: @ # :
| :::: @ # :
| :::: @ # :
+----------------------------------------------------------------------->ki
148.4 //运行时间,单位是ms Number of snapshots:
Detailed snapshots: [, , , (peak)] --------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
, ,
, ,
, ,
, ,
, ,
, ,
, ,
, ,
, ,
, ,
100.00% (,800B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->98.00% (,704B) 0xFFFFFFFFFFFFFFFF: ???
|
->02.00% (,096B) 0x4000C2F: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so) --------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
, ,
100.00% (,800B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->98.00% (,704B) 0xFFFFFFFFFFFFFFFF: ???
|
->02.00% (,096B) 0x4000C2F: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so) --------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
, ,
, ,
, ,, ,,
, ,, ,,
, ,, ,,
, ,, ,,
, ,, ,,
, ,, ,,
, ,, ,,
, ,, ,,
100.00% (,,528B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->96.88% (,,824B) 0x401B4B9: mmap (mmap.c:)
| ->94.64% (,,464B) 0x40068CB: _dl_map_object_from_fd (dl-map-segments.h:)
| | ->94.64% (,,464B) 0x4008C25: _dl_map_object (dl-load.c:)
| | ->61.86% (,,120B) 0x400DBA0: openaux (dl-deps.c:)
| | | ->61.86% (,,120B) 0x4010562: _dl_catch_error (dl-error.c:)
| | | ->61.86% (,,120B) 0x400E1E0: _dl_map_object_deps (dl-deps.c:)
| | | ->61.86% (,,120B) 0x4003A27: dl_main (rtld.c:)
| | | ->61.86% (,,120B) 0x4019630: _dl_sysdep_start (dl-sysdep.c:)
| | | ->61.86% (,,120B) 0x4001C28: _dl_start (rtld.c:)
| | | ->61.86% (,,120B) 0x4000C36: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
| | |
| | ->32.78% (,,344B) 0x4000EB3: map_doit (rtld.c:)
| | ->32.78% (,,344B) 0x4010562: _dl_catch_error (dl-error.c:)
| | ->32.78% (,,344B) 0x40020D4: handle_ld_preload (rtld.c:)
| | ->32.78% (,,344B) 0x40039AD: dl_main (rtld.c:)
| | ->32.78% (,,344B) 0x4019630: _dl_sysdep_start (dl-sysdep.c:)
| | ->32.78% (,,344B) 0x4001C28: _dl_start (rtld.c:)
| | ->32.78% (,,344B) 0x4000C36: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
| |
| ->01.34% (,016B) 0x4011773: _dl_sysdep_read_whole_file (dl-misc.c:)
| | ->01.34% (,016B) 0x4018506: _dl_load_cache_lookup (dl-cache.c:)
| | ->01.34% (,016B) 0x4009167: _dl_map_object (dl-load.c:)
| | ->01.34% (,016B) 0x400DBA0: openaux (dl-deps.c:)
| | ->01.34% (,016B) 0x4010562: _dl_catch_error (dl-error.c:)
| | ->01.34% (,016B) 0x400E1E0: _dl_map_object_deps (dl-deps.c:)
| | ->01.34% (,016B) 0x4003A27: dl_main (rtld.c:)
| | ->01.34% (,016B) 0x4019630: _dl_sysdep_start (dl-sysdep.c:)
| | ->01.34% (,016B) 0x4001C28: _dl_start (rtld.c:)
| | ->01.34% (,016B) 0x4000C36: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
| |
| ->00.89% (,344B) in + places, all below ms_print's threshold (01.00%)
|
->03.12% (,704B) 0xFFFFFFFFFFFFFFFF: ???
|
->00.00% (0B) in + places, all below ms_print's threshold (01.00%) --------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
, ,, ,,
100.00% (,,608B) (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
->96.83% (,,904B) 0x401B4B9: mmap (mmap.c:)
| ->95.87% (,,464B) 0x40068CB: _dl_map_object_from_fd (dl-map-segments.h:)
| | ->95.87% (,,464B) 0x4008C25: _dl_map_object (dl-load.c:)
| | ->62.66% (,,120B) 0x400DBA0: openaux (dl-deps.c:)
| | | ->62.66% (,,120B) 0x4010562: _dl_catch_error (dl-error.c:)
| | | ->62.66% (,,120B) 0x400E1E0: _dl_map_object_deps (dl-deps.c:)
| | | ->62.66% (,,120B) 0x4003A27: dl_main (rtld.c:)
| | | ->62.66% (,,120B) 0x4019630: _dl_sysdep_start (dl-sysdep.c:)
| | | ->62.66% (,,120B) 0x4001C28: _dl_start (rtld.c:)
| | | ->62.66% (,,120B) 0x4000C36: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
| | |
| | ->33.20% (,,344B) 0x4000EB3: map_doit (rtld.c:)
| | ->33.20% (,,344B) 0x4010562: _dl_catch_error (dl-error.c:)
| | ->33.20% (,,344B) 0x40020D4: handle_ld_preload (rtld.c:)
| | ->33.20% (,,344B) 0x40039AD: dl_main (rtld.c:)
| | ->33.20% (,,344B) 0x4019630: _dl_sysdep_start (dl-sysdep.c:)
| | ->33.20% (,,344B) 0x4001C28: _dl_start (rtld.c:)
| | ->33.20% (,,344B) 0x4000C36: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
| |
| ->00.97% (,440B) in + places, all below ms_print's threshold (01.00%)
|
->03.17% (,704B) 0xFFFFFFFFFFFFFFFF: ???
|
->00.00% (0B) in + places, all below ms_print's threshold (01.00%) --------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
, ,, ,,
, ,, ,,

参考链接:

https://youtu.be/kjkrpgWr9G4

https://blog.csdn.net/kesalin/article/details/2593958

用valgrind检查内存问题的更多相关文章

  1. 使用valgrind检查内存

    Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,是公认的最接近Purify的产品,它包含一个内核——一个软件合成的CPU,和一系列的小工具,每个工具都可以完成一项任务——调试 ...

  2. 转: 使用valgrind检查内存问题

    作者:gfree.wind@gmail.com 博客:blog.focus-linux.net   linuxfocus.blog.chinaunix.net    本文的copyleft归gfree ...

  3. valgrind 检查内存泄露

    https://www.oschina.net/translate/valgrind-memcheck

  4. Linux 下用 valgrind 查找内存泄漏小例子

    1.安装 valgrind yum install valgrind 2.测试用例 main.cpp #include <iostream> using namespace std; st ...

  5. valgrind massif内存分析[转]

    valgrind检查内存泄露 #valgrind   ./程序 内存泄漏问题,我们有memcheck工具来检查.很爽.但是有时候memcheck工具查了没泄漏,程序一跑,内存还是狂飙.这又是什么问题. ...

  6. Valgrind检测内存泄露简介

    原文地址: Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核 ...

  7. Valgrind查找内存泄露利器

    Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的ma ...

  8. valgrind调查内存leak

    快有几个月没更新了,记录一下最近解决问题用到的工具吧. 最近代码跑压力测试,总是发现内存在无规律的慢慢增加,因此在Android上用上了大名顶顶的valgrind,说实话,真是名不虚传, 真是建议以后 ...

  9. 用mtrace检查内存泄漏

    http://blog.csdn.net/ixidof/article/details/6638066内存泄漏检查方法(for Linux) 如果你更想读原始文档, 请参考glibc info的&qu ...

随机推荐

  1. TypeScript完全解读(26课时)_19.其他重要更新

    ts3.3升级过来有很多重要的更新 没法归类的更新,在本节课几种讲一下 创建update.ts,然后在index.ts内引入 async和promise es6中增加了promise的支持,能够很好处 ...

  2. 解决At least one JAR was scanned for TLDs yet contained no TLDs. 问题

    启动tomcat运行项目时,总是提示: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug loggin ...

  3. Flex Builder 装SVN

    由于Flex Builder没有内置SVN支持,很是不便.为了方便,给Flex Builder也装了SVN插件.由于FB基于Eclipse,安装方法都是一样的. 选择 Help -> Soft ...

  4. spring中的依赖注入

    Ioc的作用: 降低程序间的耦合(依赖关系) 依赖关系的管理: 以后都交给Spring来维护 在当前类需要用到其他类的对象,由Spring为我们提供, 我们只需要在配置文件中说明 依赖关系的维护,就称 ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】

    呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...

  6. UVA12504【C++STL运用】

    雨巨的UVA的C++题集英文真长- 题意: 有两本字典,第一行是旧字典,第二行是新字典. 每行不超过100个字符,没有空格,两本字典都可以是空的: 新key:+ 缺key:- 值变 :* 思路: 具体 ...

  7. bzoj 4200: [Noi2015]小园丁与老司机【dp+有上下界最小流】

    洛谷上有个点死活卡不过去,不知道是哪里写丑了orz 参考:https://www.cnblogs.com/ditoly/p/BZOJ4200.html 从上往下dp,设f为不向左右走直接上去的值,g为 ...

  8. bzoj 2007: [Noi2010]海拔【最小割+dijskstra】

    上来就跑3e5的最大流--脑子抽了 很容易看出,每个地方的海拔都是0或1因为再高了没有意义,又,上去下来再上去没有意义,所以最后一定是从s连着一片0,剩下连着t一片1,然后有贡献的就是01交接的那些边 ...

  9. IT兄弟连 JavaWeb教程 JSP内置对象2

    application对象 application对象用于保存所有应用程序中的公有数据.它在服务器启动时自动创建,在服务器关闭时销毁,当application对象没有被销毁时,所有用户都可以共享app ...

  10. bzoj4300 绝世好题 【dp】By cellur925

    题目描述: 给定一个长度为\(n\)的数列\(a\),求\(a\)的子序列\(b\)的最长长度,满足bi&bi-1!=0(\(2<=i<=len\)). 90分做法: 并没有部分分 ...