中国建设银行信用卡网站首页,江苏做网站,怎样做网站软件,政工网站建设方案一. 缘起有的公众号读者#xff0c;看完我上次写给大学生的查bug方法后#xff0c;希望我多分享一些查bug的实践经验和具体步骤#xff0c;比如如何查内存泄漏和core dump问题。所以#xff0c;就打算写这篇文章。二. 内存泄漏简介内存泄漏#xff0c;是一个谈虎色变的问题… 一. 缘起有的公众号读者看完我上次写给大学生的查bug方法后希望我多分享一些查bug的实践经验和具体步骤比如如何查内存泄漏和core dump问题。所以就打算写这篇文章。二. 内存泄漏简介内存泄漏是一个谈虎色变的问题。我个人的基础非常差大学毕业后才第一次听说内存泄漏。当时我有点懵圈心想内存泄漏了是要重新去买新的内存设备吗很傻很天真后来我又听说了很多次内存泄漏查资料后才知道原来这是一个软件层面的东西。比如使用了malloc, 但没有使用free, 或者使用了new, 但没有使用delete, 都会造成内存泄漏。为什么说内存泄漏会谈虎色变呢因为内存泄漏危害性大内存泄漏潜伏时间长内存泄漏不容易定位那么如果内存泄漏了我们该如何去定位呢在C/C相关的面试中不问这个问题的面试官是不合格的不会回答这个问题的面试者也是不合格的。当然这种说法似乎有一点绝对。有的面试者回答只有小心配对使用malloc/free和new/delete, 就能避免内存泄漏。显然这种面试者缺乏基本的工程实践认知缺乏对敌人的敬畏。有的面试者回答使用智能指针就能避免内存泄漏。显然这只是一种预防机制。在实际项目中各种复杂因素导致的后果是内存已经泄漏需要定位。还有的面试者更是想出了出人意料的答案那就是检查代码这又是对敌人缺乏了解啊。大型工程的代码动辄几十万行谁敢走读代码来查内存泄漏呢对我而言查杀bug是我的相对强项(实话说架构能力需要加强)。mtrace和valgrind是典型的内存泄漏分析工具。今天我们聊mtrace定位内存泄漏。三. mtrace简介我们来看下mtrace的用途ubuntuVM-0-15-ubuntu:~$ man mtrace
MTRACE(1) Linux user manual MTRACE(1)NAMEmtrace - interpret the malloc trace logSYNOPSISmtrace [option]... [binary] mtracedataDESCRIPTIONmtrace is a Perl script used to interpret and provide human readable output of the trace log contained inthe file mtracedata, whose contents were produced by mtrace(3). If binary is provided, the output ofmtrace also contains the source file name with line number information for problem locations (assumingthat binary was compiled with debugging information).For more information about the mtrace(3) function and mtrace script usage, see mtrace(3).
显然mtrace命令是用来分析malloc函数的trace log. 那么这个trace log是怎么生成的呢 且看上面的see mtrace(3). 有的朋友看到这里不知道怎么敲命令了以为是ubuntuVM-0-15-ubuntu:~$ man mtrace(3)
-bash: syntax error near unexpected token (
ubuntuVM-0-15-ubuntu:~$
其实正确的姿势如下ubuntuVM-0-15-ubuntu:~$ man 3 mtrace
MTRACE(3) Linux Programmers Manual MTRACE(3)NAMEmtrace, muntrace - malloc tracingSYNOPSIS#include mcheck.hvoid mtrace(void);void muntrace(void);DESCRIPTIONThe mtrace() function installs hook functions for the memory-allocation functions (malloc(3), realloc(3)memalign(3), free(3)). These hook functions record tracing information about memory allocation and deal[mlocation. The tracing information can be used to discover memory leaks and attempts to free nonallocatedmemory in a program.The muntrace() function disables the hook functions installed by mtrace(), so that tracing information isno longer recorded for the memory-allocation functions. If no hook functions were successfully installedby mtrace(), muntrace() does nothing.When mtrace() is called, it checks the value of the environment variable MALLOC_TRACE, which should con[mtain the pathname of a file in which the tracing information is to be recorded. If the pathname is suc[mcessfully opened, it is truncated to zero length.If MALLOC_TRACE is not set, or the pathname it specifies is invalid or not writable, then no hook func[mtions are installed, and mtrace() has no effect. In set-user-ID and set-group-ID programs, MALLOC_TRACEis ignored, and mtrace() has no effect.显然mtrace函数是用来记录malloc的trace log的。所以对于mtrace, 我们有如下的基本认知mtrace函数记录malloc的trace logmtrace命令分析上述记录的trace log这也就是用mtrace来定位内存泄漏的原理。那么具体如何来查内存泄漏呢不要着急继续往下看。四. 用mtrace定位内存泄漏首先我们来写一段有内存泄漏的程序#include stdio.hint main()
{setenv(MALLOC_TRACE, test.log, 1);mtrace();int *p (int *)malloc(2 * sizeof(int));return 0;
}
我们来分析一下这段程序setenv是设置相关环境变量。mtrace函数记录malloc的trace log.malloc函数用于分配堆内存我们来编译并运行一下(注意在编译时带-g参数)ubuntuVM-0-15-ubuntu:~$ gcc -g test.c
ubuntuVM-0-15-ubuntu:~$
ubuntuVM-0-15-ubuntu:~$
ubuntuVM-0-15-ubuntu:~$ ./a.out
ubuntuVM-0-15-ubuntu:~$ ls test.log
test.log
ubuntuVM-0-15-ubuntu:~$ cat test.log Start./a.out:[0x4005eb] 0x1649570 0x8/lib/x86_64-linux-gnu/libc.so.6:(clearenv0x5d)[0x7f1bc48f7e9d] - 0x1649010/lib/x86_64-linux-gnu/libc.so.6:(tdestroy0x4cf)[0x7f1bc49c291f] - 0x16490e0/lib/x86_64-linux-gnu/libc.so.6:[0x7f1bc4a3223c] - 0x1649100
显然编译运行后生成了trace log, 即test.log文件。用cat命令查看貌似也发现不了什么东西这是因为姿势错了。我们不仅仅要用test.log, 还要结合二进制文件a.out呢如下ubuntuVM-0-15-ubuntu:~$ mtrace a.out test.log
- 0x00000000018ab010 Free 3 was never allocd 0x7fb41725fe9d
- 0x00000000018ab0e0 Free 4 was never allocd 0x7fb41732a91f
- 0x00000000018ab100 Free 5 was never allocd 0x7fb41739a23cMemory not freed:
-----------------Address Size Caller
0x00000000018ab570 0x8 at /home/ubuntu/test.c:8
ubuntuVM-0-15-ubuntu:~$
Oh, nice啊终于查出是第8行存在内存泄漏。接下来我们打算修复代码并再次验证。五. 修复后再验证修复内存泄漏后代码为#include stdio.hint main()
{setenv(MALLOC_TRACE, test.log, 1);mtrace();int *p (int *)malloc(2 * sizeof(int));free(p);return 0;
}
编译运行并查看是否有内存泄漏ubuntuVM-0-15-ubuntu:~$ gcc -g test.c
ubuntuVM-0-15-ubuntu:~$
ubuntuVM-0-15-ubuntu:~$
ubuntuVM-0-15-ubuntu:~$ ./a.out
ubuntuVM-0-15-ubuntu:~$ mtrace a.out test.log
- 0x00000000006ad010 Free 4 was never allocd 0x7faa9b044e9d
- 0x00000000006ad0e0 Free 5 was never allocd 0x7faa9b10f91f
- 0x00000000006ad100 Free 6 was never allocd 0x7faa9b17f23c
No memory leaks.
ubuntuVM-0-15-ubuntu:~$
看到No memory leaks后心情就好了没有内存泄漏了。六. 最后的话无论是笔试面试还是平时工作对于内存泄漏问题都要有自己的一套处理办法。推荐阅读专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈关注公众号后台回复「1024」获取学习资料网盘链接。欢迎点赞关注转发在看您的每一次鼓励我都将铭记于心~