用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 ...
随机推荐
- myeclipse 重新关联项目和svn
有时候重装了svn或重新定义了WorkSpaces,原项目和svn没关联了 那么 右击要提交的项目 在弹出的菜单依次:Team -->share project 在弹出的对话框里填入SVN的地址 ...
- 您的Microsof Internet Explorer浏览器包含新版本的内置Adobe Flash Player。
win8 安装adobe flash player时错误提示: 您的Microsof Internet Explorer浏览器包含新版本的内置Adobe Flash Player. 感谢达人提供了如下 ...
- 打造个人IP: 开源项目网站构建框架
前言 您是否正在寻找有关如何创建博客网站: 个人博客 或者 开源项目官网 : Dubbo, Vue.js的构建框架? 在这篇文章我将向您展示如何创建一个美观并且实用的开源博客/开源项目官网构建框架!近 ...
- 洛谷 - UVA11424 - GCD - Extreme (I) - 莫比乌斯反演 - 整除分块
https://www.luogu.org/problemnew/show/UVA11424 原本以为是一道四倍经验题来的. 因为输入的n很多导致像之前那样 \(O(n)\) 计算变得非常荒谬. 那么 ...
- win10+PHP 安装memcache
1.给php环境安装memcache扩展 2.给电脑安装memcache环境 一.为win10安装memcache服务 下载对应的版本 32位系统 1.4.5版本:http://static.runo ...
- IT兄弟连 JavaWeb教程 EL表达式中的内置对象
EL语言定义了11个隐含对象,它们都是java.util.Map类型,网页制作者可通过它们来便捷地访问Web应用中的特定数据.表1对这11个隐含对象做了说明. 1 EL表达式中的内置对象 这11个隐 ...
- 【OpenJ_Bailian - 4137】最小新整数 (贪心)
最小新整数 Descriptions: 给定一个十进制正整数n(0 < n < 1000000000),每个数位上数字均不为0.n的位数为m.现在从m位中删除k位(0<k < ...
- GoldenGate对接 mysql
环境: centos 7.4 mysql 5.5.58 glibc 64 位版,下载链接:https://dev.mysql.com/downloads/mysql/5.5.html#download ...
- 小试JVM工具
一.前言 工欲善其事必先利其器,jdk自带了很多工具,利用好这些工具能够帮我们获取想要的数据(运行日志.异常堆栈.GC日志.线程快照.堆转储快照等),从而快速的分析数据.定位问题. 二.jps:虚拟机 ...
- unicode码表和标准下载 unicode官网