内存问题排查工具 --- valgrind

1 概述

在用C/C++编程的时候,经常会出现下面三种内存问题:

  • 内存泄漏
  • 悬挂指针
  • 多次释放同一块内存

本系列文章简要介绍排查这三个问题的工具和方法,先看看Valgrind

2 Valgrind

Valgrind是一款可以监测内存使用情况、监测内存泄漏的工具。对于一些规模不是很大的应用程序,Valgrind是一把利器。

3 内存泄漏监测

3.1 示例代码

 1: int main()
2: {
3: char *p = malloc(sizeof(char) * 10);
4: if (p == NULL) {
5: return 0;
6: }
7:
8: *p++ = 'a';
9: *p++ = 'b';
10:
11: printf("%s\n", *p);
12:
13: return 0;
14: }

3.2 编译它

1: gcc -g -o core1 core1.c

3.3 用Valgrind监测进程的内存泄漏

1: valgrind --leak-check=yes --show-reachable=yes ./core

Valgrind的输出为为:

 1: ==25500== Memcheck, a memory error detector
2: ==25500== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
3: ==25500== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
4: ==25500== Command: ./core1
5: ==25500==
6: ==25500== Conditional jump or move depends on uninitialised value(s)
7: ==25500== at 0x36A104546A: vfprintf (in /lib64/libc-2.12.so)
8: ==25500== by 0x36A104EAC9: printf (in /lib64/libc-2.12.so)
9: ==25500== by 0x40055D: main (core1.c:13)
10: ==25500==
11: (null)
12: ==25500==
13: ==25500== HEAP SUMMARY:
14: ==25500== in use at exit: 10 bytes in 1 blocks
15: ==25500== total heap usage: 1 allocs, 0 frees, 10 bytes allocated
16: ==25500==
17: ==25500== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
18: ==25500== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
19: ==25500== by 0x400515: main (core1.c:5)
20: ==25500==
21: ==25500== LEAK SUMMARY:
22: ==25500== definitely lost: 10 bytes in 1 blocks
23: ==25500== indirectly lost: 0 bytes in 0 blocks
24: ==25500== possibly lost: 0 bytes in 0 blocks
25: ==25500== still reachable: 0 bytes in 0 blocks
26: ==25500== suppressed: 0 bytes in 0 blocks
27: ==25500==
28: ==25500== For counts of detected and suppressed errors, rerun with: -v
29: ==25500== Use --track-origins=yes to see where uninitialised values come from
30: ==25500== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)

可以看到,Valgrind提示在第五行分配的内存未被释放

4 悬挂指针

4.1 示例代码

 1: struct elem {
2: int a;
3: double b;
4: };
5:
6: int main()
7: {
8: struct elem *e = malloc(sizeof(struct elem));
9: if (e == NULL) {
10: return 0;
11: }
12:
13: e->a = 10;
14: e->b = 10.10;
15:
16: double *xx = &e->b;
17:
18: printf("%f\n", *xx);
19:
20: free(e);
21:
22: printf("%f\n", *xx);
23:
24: return 0;
25: }

4.2 Valgrind运行结果

同样用-g编译后valgrind运行的结果:

 1: [cobbliu@MacBook]$ valgrind --leak-check=yes --show-reachable=yes ./core2
2: ==26148== Memcheck, a memory error detector
3: ==26148== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
4: ==26148== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
5: ==26148== Command: ./core2
6: ==26148==
7: 10.100000
8: ==26148== Invalid read of size 8
9: ==26148== at 0x4005CA: main (core2.c:26)
10: ==26148== Address 0x502a048 is 8 bytes inside a block of size 16 free'd
11: ==26148== at 0x4A04D72: free (vg_replace_malloc.c:325)
12: ==26148== by 0x4005C5: main (core2.c:24)
13: ==26148==
14: 10.100000
15: ==26148==
16: ==26148== HEAP SUMMARY:
17: ==26148== in use at exit: 0 bytes in 0 blocks
18: ==26148== total heap usage: 1 allocs, 1 frees, 16 bytes allocated
19: ==26148==
20: ==26148== All heap blocks were freed -- no leaks are possible
21: ==26148==
22: ==26148== For counts of detected and suppressed errors, rerun with: -v
23: ==26148== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)

可以看到在free(e)后,指针xx成为了悬挂指针,此后对xx的读,如果xx指向的内存还未被glibc回收,进程不会core掉。valgrind提示在26行做了对xx的 Invalid read.

5 多次释放同一个指针

5.1 示例代码

 1: int main()
2: {
3: char *p = malloc(sizeof(char) * 10);
4: if (p == NULL) {
5: return 0;
6: }
7:
8: char *q = p;
9:
10: *p++ = 'a';
11: *p++ = 'b';
12:
13: printf("%s\n", *p);
14:
15: free(p);
16: free(q);
17: return 0;
18: }

5.2 Valgrind 监测

 1: [cobbliu@MacBook]$ valgrind --leak-check=yes --show-reachable=yes ./core1
2: ==26874== Memcheck, a memory error detector
3: ==26874== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
4: ==26874== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
5: ==26874== Command: ./core1
6: ==26874==
7: ==26874== Conditional jump or move depends on uninitialised value(s)
8: ==26874== at 0x36A104546A: vfprintf (in /lib64/libc-2.12.so)
9: ==26874== by 0x36A104EAC9: printf (in /lib64/libc-2.12.so)
10: ==26874== by 0x4005B5: main (core1.c:15)
11: ==26874==
12: (null)
13: ==26874== Invalid free() / delete / delete[]
14: ==26874== at 0x4A04D72: free (vg_replace_malloc.c:325)
15: ==26874== by 0x4005C1: main (core1.c:17)
16: ==26874== Address 0x502a042 is 2 bytes inside a block of size 10 alloc'd
17: ==26874== at 0x4A0515D: malloc (vg_replace_malloc.c:195)
18: ==26874== by 0x400565: main (core1.c:5)
19: ==26874==
20: ==26874==
21: ==26874== HEAP SUMMARY:
22: ==26874== in use at exit: 0 bytes in 0 blocks
23: ==26874== total heap usage: 1 allocs, 2 frees, 10 bytes allocated
24: ==26874==
25: ==26874== All heap blocks were freed -- no leaks are possible
26: ==26874==
27: ==26874== For counts of detected and suppressed errors, rerun with: -v
28: ==26874== Use --track-origins=yes to see where uninitialised values come from
29: ==26874== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)

可以看到,valgrind提示在17行有一个Invalid Free()

6 Valgrind的优缺点

valgrind默认使用memcheck工具做内存监测。

6.1 Advantages

用valgrind监测内存泄漏,不用重新编译应用程序,不用重新链接应用程序,不用对应用进程做任何修改。如果想查看详细的出错信息,只需要在编译时加上-g选项。

6.2 Disadvantages

不管valgrind在使用memcheck工具监测内存时,它会接管应用程序,并且读取应用程序可执行文件和库文件中的debug信息来显示详细的出错位置。当valgrind启动后,应用 进程实际上在valgrind的虚拟环境中执行,valgrind会将每行代码传递给memcheck工具,memcheck工具再加入自己的调试信息,之后再将合成的代码真正运行。memcheck工具在 应用进程每个防存操作和每个变量赋值操作时加入额外的统计代码,通常情况下,使用memcheck工具后应用程序的运行时间会比原生代码慢大约10-50倍。

其次,对于一些不停机运行的服务器程序的内存问题,valgrind无能为力。不仅仅是因为valgrind无法使之停止,还有可能是因为服务器进程本身就被设计为申请一些生命周期 与进程生命周期一样长的内存,永远不释放,这些内存会被valgrind报泄漏错误。

再次,valgrind对多线程程序支持得不够好。在多线程程序执行时,valgrind在同一时刻只让其中一个线程执行,它不会充分利用多核的环境。在用valgrind运行您的多线程程序 时,您的宝贵程序的运行情况可能跟不使用valgrind的运行情况千差万别。

7 Valgrind的其他工具

除了memcheck工具外,valgrind工具包还有一些别的好用的工具

7.1 Cachegrind

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

7.2 Callgrind

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

7.3 Helgrind

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

7.4 DRD

这也使一款多线程程序监测工具,它提供的监测信息比Helgrind更丰富。 详情见这里

7.5 Massif

堆栈分析器,它能测量程序在堆栈中使用了多少内存。告诉我们堆块,堆管理块和栈的大小。对于那些被应用进程释放但是还没有交还给操作系统的内存,memcheck是监测 不出来的,而Massif能有效第监测到这类内存。 详情见这里

7.6 DHAT

这个工具能详细地显示应用进程如何使用堆栈,以使用户更好地评估程序的设计。 详情见这里

Author: Cobbliu

Created: 2015-04-14 Tue 01:19

Emacs 24.4.1 (Org mode 8.2.10)

Validate

 

内存排查 valgrind的更多相关文章

  1. Java 内存排查,慢慢收集总结

    Java堆外内存排查小结: https://mp.weixin.qq.com/s?__biz=MzA4MTc4NTUxNQ==&mid=2650518612&idx=2&sn= ...

  2. linux内存排查工具valgrind

    官网:http://valgrind.org/info/about.html 百科介绍:http://baike.baidu.com/link?url=ZdXzff0omzoPpE_yZUlNW9lJ ...

  3. 各个函数消耗的时间profiling和内存泄漏valgrind

    来源:http://06110120wxc.blog.163.com/blog/static/37788161201333112445844/ ARM(hisi)上面的profiling和valgri ...

  4. linux下内存调试工具——valgrind

    1.valgrind之memcheck  最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获.所以,它 ...

  5. C++ 内存分析-valgrind

    valgrind包括了以下几个比较重要的模块:memcheck, cachegrind, callgrind, helgrind, drd, massif, dhat, sgcheck, bbv. 还 ...

  6. 实战经验 | Cassandra Java堆外内存排查经历全记录

    背景 最近准备上线cassandra这个产品,同事在做一些小规格ECS(8G)的压测.压测时候比较容易触发OOM Killer,把cassandra进程干掉.问题是8G这个规格我配置的heap(Xmx ...

  7. 超干货!Cassandra Java堆外内存排查经历全记录

    背景 最近准备上线cassandra这个产品,同事在做一些小规格ECS(8G)的压测.压测时候比较容易触发OOM Killer,把cassandra进程干掉.问题是8G这个规格我配置的heap(Xmx ...

  8. linux 内存泄露 valgrind

    内泄漏工具 valgrind: https://linux.die.net/man/1/valgrind www.valgrind.org/docs/manual/index.html www.val ...

  9. Linux 内存泄漏 valgrind

    Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/curre ...

随机推荐

  1. Swift继承

    //声明一个基类 class vehicle { var maxPassenger : Int = 0 var manufacturer : String! func description() -& ...

  2. UVA 847 - A Multiplication Game(游戏)

    UVA 847 - A Multiplication Game 题目链接 题意:一个数一開始是1,每次轮流乘2-9,谁先大于n谁就赢,问谁胜 思路:博弈,找出必胜态.2-9为stan,10-18为ol ...

  3. poj2096--Collecting Bugs(可能性dp第二弹,需求预期)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2678   Accepted: 1302 ...

  4. Linux 字符驱动程序(一)

    Linux 字符驱动程序(一) 于linux有三个主要的内核设备: 1 字符设备:         •字符设备的读写以字节为单位,存取时没有缓存.      •对字符设备发出读写请求时.实际的硬件I/ ...

  5. linux下一个php未找到php型材php.ini解决方案

    编译并安装自己php经常会遇到这样的问题.我找不到php.ini.对于根据下面的方法可以解决例: 首先是需要解释.假设你php它被编译并安装,那么默认是没有php.ini的,你必须得去复制源代码包内. ...

  6. USACO maze1 BFS

    不写了很长的时间bfs该,很长一段时间的中间失误,当延期一次延伸成功的新节点的节点应该被标记为参观.否则,在某些情况下无限期延长队列. 输入一个小坑爹处理称号,能够进来当字符串被读取.然后用周围的墙上 ...

  7. HDU 3037 Saving Beans (Lucas法则)

    主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...

  8. mybatis generator插件开发

    mybatis现在普遍使用的每一个人DAO框架.mybatis generator它可以基于数据库中的表结构,生成自己主动mybatis代码和配置文件,方便使用,当然,实际的使用过程中.generat ...

  9. Android Context MODE的四种模式解析(转)

    Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中.可以使用Context ...

  10. 3D数学学习笔记——笛卡尔坐标系

    本系列文章由birdlove1987编写.转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/24601215 1.3D数学 ...