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. 【原创】Gitbook使用

    [常用命令] 1.gitbook install 安装依赖模块 2.gitbook build 编译,结果输出在_book文件夹下 3.gitbook serve 本机预览,默认端口为4000 [注意 ...

  2. 服务器无法在发送 HTTP 标头之后修改 cookie

    隔三差五就碰到VS报错: System.Web.HttpException:“服务器无法在发送 HTTP 标头之后修改 cookie.” 解决后过几天又忘记了. 原因是: 程序为每个页面在config ...

  3. HTML学习笔记(五)框架

    框架 通过使用框架,可以在同一个浏览器窗口中显示多个页面. eg: <frameset rows="50%,50%"> <frame src="/exa ...

  4. Unity3D中调用外接摄像头,并保存为图片文件

    http://bbs.9ria.com/thread-170539-1-1.html 项目要求调用摄像头,并且把图像保存下来,上传到服务器. 这里有几个难点,调用摄像头是很简单的,unity已经提供好 ...

  5. [Xcode 实际操作]九、实用进阶-(19)重写父类的绘图方法,使用图形上下文绘制自定义图形

    目录:[Swift]Xcode实际操作 本文将演示如何使用图形上下文,绘制自定义图形. 使用快捷键[Command]+[N]创建一个新的类文件. (在项目文件夹[DemoApp]上点击鼠标右键[New ...

  6. 【BZOJ 3233】 [Ahoi2013]找硬币

    [题目 描述] 小蛇是金融部部长. 最近她决定制造一系列新的货币. 假设她要制造的货币 的面值为 x1, x2, x3… 那么 x1 必须为 1, xb 必须为 xa 的正整数倍(b>a). 例 ...

  7. Windows个人常用软件推荐

    一.必装软件 浏览器:Google chrome Google Chrome是一款可让您更快速.轻松且安全地使用网络的浏览器,它的设计超级简洁,使用起来更加方便,支持多标签浏览,同时也支持扩展插件.下 ...

  8. C# 委托之把委托从委托链(多播委托)移除

    运用“-”运算符将委托从委托链移除 class HelloWorld { //定义委托类型 delegate void DelegationChain(); static void Main(stri ...

  9. SQLachemy基础

    SQLAchemy SQLAchemy是python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作, 简言之便是:将对象转换成SQL,然后使用数据API执行S ...

  10. ZROI #365. 【2018普转提day18专题】嘤嘤嘤嘤

    ZROI #365. [2018普转提day18专题]嘤嘤嘤嘤 直接放代码 具体做法见注释 #include<stdio.h> #include<cstring> #inclu ...