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. Win10 VC++运行库集合|VC++ 2005 2008 2010 2012 2015

    在Win10系统中很多朋友在运行一些软件时会遇到缺少.DLL的情况,主要是没有安装VC++运行库下面小编收集了Win10 VC++运行库集合,大家安装上去就可以了~ 微软常用软件运行库合集(vc201 ...

  2. 手把手教你使用ueditor

    ueditor的强大功能就不再一一叙述了,我们的目的就是通过使用php与html实现下面的效果 话不多说,上干货 前言:文件都是基于tp5的 1.引入富文本编辑器 将 ueditor 下的文件引入 1 ...

  3. Android NDK开发指南(二)Android.mk文件

    http://www.cnblogs.com/yaozhongxiao/archive/2012/03/06/2382225.html 1.  概述 Android.mk文件是用来描述build sy ...

  4. android摄像头获取图像——第三弹

    相机获取图像的格式问题 android中承认的格式的参考网址为 :http://developer.android.com/reference/android/graphics/ImageFormat ...

  5. 编译keepalived 方法

    作者的环境: redhat 6.5 64 位版 在编译keepalived 前,需要提前给环境安装两个依赖包--zlib和openssl 编译 zlib 库 参考作者之前的博客 http://www. ...

  6. NPOI用WorkbookFactory读写 2007以上格式文件(xlsx)

    //我用的最新的2.2.1版本 //第一步:引用DLL,5个全导入,包括ICSHARP.ZIP,是个开源压缩工具包.XLSX是压缩格式,需要它来解压 //第二部: using NPOI.SS.User ...

  7. react native设置容器阴影

    shadowColor:'#eee',shadowOffset:{h:10,w:10},shadowRadius:3,shadowOpacity:0.8,

  8. foreach循环报NPE空指针异常

    前言 最近debug时忽然发现,如果一个集合赋值为null,那么对该集合进行foreach循环(也叫增强for循环)时,会报NPE(即空指针异常NullPointerException). 代码如下: ...

  9. C#静态类、静态构造函数,类与结构体的比较

    一.静态类 静态类是不能实例化的,我们直接使用它的属性与方法,静态类最大的特点就是共享. 探究 public static class StaticTestClass{    public stati ...

  10. [51Nod1952] 栈

    Description 不支持后端删除的dequeue,每次操作后查询最大值. \(n\leq10^7\).时限1.5s,不用考虑读入/输出复杂度. Solution 首先考虑如果没有后端删除怎么做, ...