用valgrind检查内存问题
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://blog.csdn.net/kesalin/article/details/2593958
用valgrind检查内存问题的更多相关文章
- 使用valgrind检查内存
Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,是公认的最接近Purify的产品,它包含一个内核——一个软件合成的CPU,和一系列的小工具,每个工具都可以完成一项任务——调试 ...
- 转: 使用valgrind检查内存问题
作者:gfree.wind@gmail.com 博客:blog.focus-linux.net linuxfocus.blog.chinaunix.net 本文的copyleft归gfree ...
- valgrind 检查内存泄露
https://www.oschina.net/translate/valgrind-memcheck
- Linux 下用 valgrind 查找内存泄漏小例子
1.安装 valgrind yum install valgrind 2.测试用例 main.cpp #include <iostream> using namespace std; st ...
- valgrind massif内存分析[转]
valgrind检查内存泄露 #valgrind ./程序 内存泄漏问题,我们有memcheck工具来检查.很爽.但是有时候memcheck工具查了没泄漏,程序一跑,内存还是狂飙.这又是什么问题. ...
- Valgrind检测内存泄露简介
原文地址: Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核 ...
- Valgrind查找内存泄露利器
Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的ma ...
- valgrind调查内存leak
快有几个月没更新了,记录一下最近解决问题用到的工具吧. 最近代码跑压力测试,总是发现内存在无规律的慢慢增加,因此在Android上用上了大名顶顶的valgrind,说实话,真是名不虚传, 真是建议以后 ...
- 用mtrace检查内存泄漏
http://blog.csdn.net/ixidof/article/details/6638066内存泄漏检查方法(for Linux) 如果你更想读原始文档, 请参考glibc info的&qu ...
随机推荐
- 2、webpack基础配置
我们需要安装webpack 还需要安装webpack cli 这两个都是我们的开发依赖 这里我们一般会加一个-D表示上线的时候不需要他们两个包 安装我们的webpack 先初始化一下,记住我们的安装依 ...
- 01 mybatis框架整体概况(2018.7.10)-
01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...
- Ubuntu下对与rtl8723be网卡频繁断网问题解决
linux下对于rtl系列的无线网卡,大多数网友都在吐槽,总是频繁的掉网,就此将自己在网上安装时的经验写下. 1.下载网卡驱动,其中包含rtl的大多数包 sudo apt-get install li ...
- ASP.NET中MessageBox的实现
asp.net中没有MessageBox这个控件,固然可以插入Winform里的MessageBox,但一般不提倡,所以只能变通实现,主要有这几种方法: 1.直接利用javascript的alert和 ...
- Redis缓存雪崩、缓存穿透、缓存击穿、缓存降级、缓存预热、缓存更新
Redis缓存能够有效地加速应用的读写速度,就DB来说,Redis成绩已经很惊人了,且不说memcachedb和Tokyo Cabinet之流,就说原版的memcached,速度似乎也只能达到这个级别 ...
- Unity3D研究院之手游开发中所有特殊的文件夹
这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor 和 /Edi ...
- 五分钟了解Mecanim角色动画系统
http://www.narkii.com/club/thread-305414-1.html Unity 4.0推出的Mecanim动画系统已经有一段时间,不过据了解很多的朋友仍然在使用原来的角色动 ...
- Ogre 简易角色Demo
参考Sample中的角色类,使用ExampleApplication.h改写,只编译了release,debug在配置文件名字上有不同 遗留一个问题 mBodyEnt->getSkeleton( ...
- 51nod1247(gcd)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1247 题意:中文题诶- 思路:(a, b)可以直接到达(a+b ...
- [Xcode 实际操作]一、博主领进门-(8)应用代理文件(AppDelegate.swift)详解
目录:[Swift]Xcode实际操作 本文将演示使用iOS模拟器,演示程序的生命周期. 在项目导航区,打开应用代理文件[AppDelegate.swift] 应用代理文件时系统运行本应用的委托,里面 ...