一. 缘起
年前,写了使用mtrace定位内存泄漏,在留言中,有读者提到了希望介绍valgrind,那好,今天就介绍使用valgrind定位内存泄漏。
大约2-3年前,杨同学让我帮做模拟面试,他求职的是C++岗位,我问了这样一个问题:在你的项目中,你是如何定位内存泄漏的呢?
结果,他对这个问题很陌生,感觉从来没有思考过相关问题,也没有做这方面的准备,自然就没法正确作答,这让我觉得有点吃惊。
如果准备得不成功,那就要准备失败了。在笔试面试中,遇到内存泄漏的定位问题,如果连valgrind都说不出来,那就很容易歇菜了。
总之,无论是为了找工作,还是为了实际工作中的问题,都很有必要熟练使用valgrind,那么,我们一起来看看这玩意儿是怎么回事。
二. valgrind简介
有的朋友可能还不熟悉linux, 没关系,和涛哥一起来,先来man valgrind一下:
ubuntu@VM-0-15-ubuntu:~$ man valgrindVALGRIND(1) Release 3.11.0 VALGRIND(1)NAMEvalgrind - a suite of tools for debugging and profiling programsSYNOPSISvalgrind [valgrind-options] [your-program] [your-program-options]DESCRIPTIONValgrind is a flexible program for debugging and profiling Linux executables. It consists of a core, whichprovides a synthetic CPU in software, and a series of debugging and profiling tools. The architecture is modular,so that new tools can be created easily and without disturbing the existing structure.Some of the options described below work with all Valgrind tools, and some only work with a few or one. Thesection MEMCHECK OPTIONS and those below it describe tool-specific options.

这篇博客介绍了如何利用valgrind工具定位内存泄漏,包括valgrind的基本概念、功能,以及通过实例演示如何使用memcheck工具检测内存泄漏,并在修复后验证内存泄漏问题是否已解决。
754

被折叠的 条评论
为什么被折叠?



