系列文章目录
前言
该篇主要介绍开源的内存泄漏工具 valgrind,valgrind 是一套 Linux 下,开放源代码的动态调试工具集合,能够检测内存管理错误、线程 BUG 等,valgrind 由内核(core)以及基于内核的其他调试工具组成。内核类似于一个框(framework),它模拟了一个 CPU 环境,并提供服务给其他工具;而其他工具则类似于插件 (plug-in),利用内核提供的服务完成各种特定的内存调试任务。
一、valgrind 是什么?
Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合。Valgrind由内核(core)以及基于内核的其他调试工具组成。内核类似于一个框架(framework),它模拟了一个CPU环境,并提供服务给其他工具;而其他工具则类似于插件 (plug-in),利用内核提供的服务完成各种特定的内存调试任务。
valgrind包括的工具如下:
memcheck,这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
callgrind,主要用来检查程序中函数调用过程中出现的问题。
cachegrind,主要用来检查程序中缓存使用出现的问题。
helgrind,主要用来检查多线程程序中出现的竞争问题。
massif,主要用来检查程序中堆栈使用中出现的问题。
extension,可以利用core提供的功能,自己编写特定的内存调试工具。
valgrind有很多功能,本文仅仅介绍了一种最基本的应用。
二、使用步骤
1.引入库
wget https://sourceware.org/pub/valgrind/valgrind-3.2.1.tar.bz2 --no-check-certificate
解压
tar -jxvf valgrind-3.14.0.tar.bz2
安装目录
cd valgrind-3.14.0
./configure --prefix=/home/user1/valgrind
make
make install
//设置环境变量
vim ~/.bashrc
export PATH=$PATH:~/valgrind/bin/
source ~/.bashrc
或者安装通过
yum install valgrind
如下方式来观察内存泄漏
valgrind --tool=memcheck --leak-check=full --error-limit=no --num-callers=50 --show-reachable=yes --log-file=./valgrind_report.log ./mytest
--tool=memcheck : 使用内存检测的工具
--leak-check=full : 有设置这个选项时,当被测程序结束时查找内存泄露。设置为summary,Valgrind会统计有多少内存泄露发生,若设置为full、yes,Valgrind会给出每一个独立的泄露的详细信息
--error-limit=no:如果错误太多要停止显示新的错误的选项
--num-callers=50:这个值默认是12,最高是50。表示显示多少层的堆栈,设置越高会使Valgrind运行越慢而且使用更多的内存,但是在嵌套调用层次比较高的程序中非常实用
--log-file=/home/davit/valgrind_report.log: 指定检测结果报告的存放路径和文件名
./xxxx: 执行被测可执行文件
--show-reachable=yes
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <fstream>
int fun(char* pData,int size)
{
const char *recordpath = "/opt/eDisk/davit.test";
std::ofstream outfile;
outfile.open(recordpath, std::ios_base::out|std::ios_base::app);
outfile.write((const char*) pData, size);
return 0;
}
int memLeakage()
{
char *pMemory = new char [1024];
memset(pMemory,1,1024);
fun(pMemory,1024);
}
int main(int argc, char* argv[])
{
memLeakage();
sleep(60);
return 0;
}
编译:
g++ test.cpp -o mytest -std=c++11 -g -fno-elide-constructors
内存泄漏检测:
valgrind --tool=memcheck --leak-check=full --error-limit=no --num-callers=50 --show-reachable=yes --log-file=./valgrind_report.log ./mytest
==29711== Memcheck, a memory error detector
==29711== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==29711== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==29711== Command: ./mytest
==29711== Parent PID: 17468
==29711==
==29711==
==29711== Process terminating with default action of signal 2 (SIGINT)
==29711== at 0x571C9E0: __nanosleep_nocancel (in /usr/lib64/libc-2.17.so)
==29711== by 0x571C893: sleep (in /usr/lib64/libc-2.17.so)
==29711== by 0x40124B: main (test.cpp:339)
==29711==
==29711== HEAP SUMMARY:
==29711== in use at exit: 1,024 bytes in 1 blocks
==29711== total heap usage: 3 allocs, 2 frees, 9,784 bytes allocated
==29711==
==29711== 1,024 bytes in 1 blocks are definitely lost in loss record 1 of 1
==29711== at 0x4C2AC38: operator new[](unsigned long) (vg_replace_malloc.c:433)
==29711== by 0x401200: memLeakage() (test.cpp:331)
==29711== by 0x401241: main (test.cpp:338)
==29711==
==29711== LEAK SUMMARY:
==29711== definitely lost: 1,024 bytes in 1 blocks
==29711== indirectly lost: 0 bytes in 0 blocks
==29711== possibly lost: 0 bytes in 0 blocks
==29711== still reachable: 0 bytes in 0 blocks
==29711== suppressed: 0 bytes in 0 blocks
==29711==
==29711== For lists of detected and suppressed errors, rerun with: -s
==29711== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
通过检测报告,本代码种存在一个内存泄漏的问题,即函数memLeakge存在内存泄漏,这样就很容易判断问题了。
–
总结
本文就是抛砖引玉,希望你能够更好的使用valgrind,它能够帮你解决很多问题。