“该死系统存在内存泄漏问题”,项目中因为各方面因素,老是有人抱怨存在内存泄漏,系统长时间运行以后,可用内存愈来愈少,甚至致使了某些服务失败。内存泄漏是最难发现的常见错误之一,由于除非用完内存或调用malloc失败,不然都不会致使任何问题。实际上,使用C/C++这类没有垃圾回收机制的语言时,你不少时间都花在处理如何正确释放内存上。若是程序运行时间足够长,如后台进程运行在服务器上,只要服务器不宕机就一直运行,一个小小的失误也会对程序形成重大的影响,如形成某些关键服务失败。php
对于内存泄漏,本人深有体会!实习的时候,公司一个项目中就存在内存泄漏问题,项目的代码量很是大,后台进程也比较多,形成内存泄漏的地方比较难找。此次机会是我对如何查找内存泄漏问题,有了必定的经验,后面本身的作了相关实验,在此我分享一下内存泄漏如何调试查找,主要内容以下:html
其实Windows、Linux下面的内存检测均可以单独开篇详细介绍,方法和工具也远不止文中介绍到的,个人方法不是最优的,若是您有更好的方法,也请您告诉我和你们。ios
wikipedia中这样定义内存泄漏:在计算机科学中,内存泄漏指因为疏忽或错误形成程序未能释放已经再也不使用的内存的状况。内存泄漏并不是指内存在物理上的消失,而是应用程序分配某段内存后,因为设计错误,致使在释放该段内存以前就失去了对该段内存的控制,从而形成了内存的浪费。程序员
最难捉摸也最难检测到的错误之一是内存泄漏,即未能正确释放之前分配的内存的 bug。 只发生一次的小的内存泄漏可能不会被注意,但泄漏大量内存的程序或泄漏日益增多的程序可能会表现出各类征兆:从性能不良(而且逐渐下降)到内存彻底用尽。 更糟的是,泄漏的程序可能会用掉太多内存,以至另外一个程序失败,而使用户无从查找问题的真正根源。 此外,即便无害的内存泄漏也多是其余问题的征兆。编程
内存泄漏会由于减小可用内存的数量从而下降计算机的性能。最终,在最糟糕的状况下,过多的可用内存被分配掉致使所有或部分设备中止正常工做,或者应用程序崩溃。内存泄漏可能不严重,甚至可以被常规的手段检测出来。在现代操做系统中,一个应用程序使用的常规内存在程序终止时被释放。这表示一个短暂运行的应用程序中的内存泄漏不会致使严重后果。服务器
在如下情況,内存泄漏致使较严重的后果:less
下面咱们经过如下例子来介绍如何检测内存泄漏问题:函数
1 #include <stdlib.h> 2 #include <iostream> 3 using namespace std; 4 5 void GetMemory(char *p, int num) 6 { 7 p = (char*)malloc(sizeof(char) * num);//使用new也可以检测出来 8 } 9 10 int main(int argc,char** argv) 11 { 12 char *str = NULL; 13 GetMemory(str, 100); 14 cout<<"Memory leak test!"<<endl; 15 //若是main中存在while循环调用GetMemory 16 //那么问题将变得很严重 17 //while(1){GetMemory(...);} 18 return 0; 19 }
实际中不可能这么简单,若是这么简单也用不着别的方法,程序员一眼就能够看出问题,此程序只用于测试。工具
Windows平台下面Visual Studio调试器和C运行时(CRT)库为咱们提供了检测和识别内存泄漏的有效方法,原理大体以下:内存分配要经过CRT在运行时实现,只要在分配内存和释放内存时分别作好记录,程序结束时对比分配内存和释放内存的记录就能够肯定是否是有内存泄漏。在vs中启用内存检测的方法以下:性能
1 #define _CRTDBG_MAP_ALLOC 2 #include <stdlib.h> 3 #include <crtdbg.h>
经过包括 crtdbg.h,将 malloc 和 free 函数映射到它们的调试版本,即 _malloc_dbg 和 _free_dbg,这两个函数将跟踪内存分配和释放。 此映射只在调试版本(在其中定义了_DEBUG)中发生。 发布版本使用普通的 malloc 和 free 函数。
#define 语句将 CRT 堆函数的基版本映射到对应的“Debug”版本。 并不是绝对须要该语句;但若是没有该语句,内存泄漏转储包含的有用信息将较少。
_CrtDumpMemoryLeaks();
此时,完整的代码以下:
1 #define _CRTDBG_MAP_ALLOC 2 #include <stdlib.h> 3 #include <crtdbg.h> 4 5 #include <iostream> 6 using namespace std; 7 8 void GetMemory(char *p, int num) 9 { 10 p = (char*)malloc(sizeof(char) * num); 11 } 12 13 int main(int argc,char** argv) 14 { 15 char *str = NULL; 16 GetMemory(str, 100); 17 cout<<"Memory leak test!"<<endl; 18 _CrtDumpMemoryLeaks(); 19 return 0; 20 }
当在调试器下运行程序时,_CrtDumpMemoryLeaks 将在“输出”窗口中显示内存泄漏信息。 内存泄漏信息以下所示:
Detected memory leaks! Dumping objects -> d:\documents\visualstudio2013\projects\memoryleak\memoryleak\main.cpp(10) : {164} normal block at 0x0033C778, 100 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. 程序“[8800] MemoryLeak.exe”已退出,返回值为 0 (0x0)。
若是没有使用#define _CRTDBG_MAP_ALLOC 语句,内存泄漏转储将以下所示:
Detected memory leaks! Dumping objects -> {164} normal block at 0x0076C778, 100 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. 程序“[12384] MemoryLeak.exe”已退出,返回值为 0 (0x0)。
未定义_CRTDBG_MAP_ALLOC 时,所显示的会是:
从不会在内存泄漏信息中看到下面两种块类型:
定义了 _CRTDBG_MAP_ALLOC 时,还会显示在其中分配泄漏的内存的文件。 文件名后括号中的数字(本示例中为 10)是该文件中的行号。
注意:若是程序老是在同一位置退出,调用 _CrtDumpMemoryLeaks 将很是容易。 若是程序从多个位置退出,则无需在每一个可能退出的位置放置对 _CrtDumpMemoryLeaks 的调用,而能够在程序开始处包含如下调用:
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
该语句在程序退出时自动调用 _CrtDumpMemoryLeaks。 必须同时设置 _CRTDBG_ALLOC_MEM_DF 和_CRTDBG_LEAK_CHECK_DF 两个位域,如前面所示。
经过上面的方法,咱们几乎能够定位到是哪一个地方调用内存分配函数malloc和new等,如上例中的GetMemory函数中,即第10行!可是不能定位到,在哪一个地方调用GetMemory()致使的内存泄漏,并且在大型项目中可能有不少处调用GetMemory。如何要定位到在哪一个地方调用GetMemory致使的内存泄漏?
定位内存泄漏的另外一种技术涉及在关键点对应用程序的内存状态拍快照。 CRT 库提供一种结构类型 _CrtMemState,您可用它存储内存状态的快照:
_CrtMemState s1, s2, s3;
若要在给定点对内存状态拍快照,请向 _CrtMemCheckpoint 函数传递 _CrtMemState 结构。 该函数用当前内存状态的快照填充此结构:
_CrtMemCheckpoint( &s1 );
经过向 _CrtMemDumpStatistics 函数传递 _CrtMemState 结构,能够在任意点转储该结构的内容:
_CrtMemDumpStatistics( &s3 );
若要肯定代码中某一部分是否发生了内存泄漏,能够在该部分以前和以后对内存状态拍快照,而后使用 _CrtMemDifference 比较这两个状态:
1 _CrtMemCheckpoint( &s1 ); 2 // memory allocations take place here 3 _CrtMemCheckpoint( &s2 ); 4 5 if ( _CrtMemDifference( &s3, &s1, &s2) ) 6 _CrtMemDumpStatistics( &s3 );
顾名思义,_CrtMemDifference 比较两个内存状态(s1 和 s2),生成这两个状态之间差别的结果(s3)。 在程序的开始和结尾放置 _CrtMemCheckpoint 调用,并使用_CrtMemDifference 比较结果,是检查内存泄漏的另外一种方法。 若是检测到泄漏,则可使用 _CrtMemCheckpoint 调用经过二进制搜索技术来划分程序和定位泄漏。
如上面的例子程序咱们能够这样来定位确切的调用GetMemory的地方:
1 #define _CRTDBG_MAP_ALLOC 2 #include <stdlib.h> 3 #include <crtdbg.h> 4 5 #include <iostream> 6 using namespace std; 7 8 _CrtMemState s1, s2, s3; 9 10 void GetMemory(char *p, int num) 11 { 12 p = (char*)malloc(sizeof(char) * num); 13 } 14 15 int main(int argc,char** argv) 16 { 17 _CrtMemCheckpoint( &s1 ); 18 char *str = NULL; 19 GetMemory(str, 100); 20 _CrtMemCheckpoint( &s2 ); 21 if ( _CrtMemDifference( &s3, &s1, &s2) ) 22 _CrtMemDumpStatistics( &s3 ); 23 cout<<"Memory leak test!"<<endl; 24 _CrtDumpMemoryLeaks(); 25 return 0; 26 }
调试时,程序输出以下结果:
0 bytes in 0 Free Blocks. 100 bytes in 1 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 0 bytes. Total allocations: 100 bytes. Detected memory leaks! Dumping objects -> d:\documents\visualstudio2013\projects\memoryleak\memoryleak\main.cpp(12) : {164} normal block at 0x0078C778, 100 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. 程序“[10264] MemoryLeak.exe”已退出,返回值为 0 (0x0)。
这说明在s1和s2之间存在内存泄漏!!!若是GetMemory不是在s1和s2之间调用,那么就不会有以下信息输出。
0 bytes in 0 Free Blocks. 100 bytes in 1 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 0 bytes. Total allocations: 100 bytes.
在上面咱们介绍了,vs中在代码中“包含crtdbg.h,将 malloc 和 free 函数映射到它们的调试版本,即 _malloc_dbg 和 _free_dbg,这两个函数将跟踪内存分配和释放。 此映射只在调试版本(在其中定义了_DEBUG)中发生。 发布版本使用普通的 malloc 和 free 函数。”即为malloc和free作了钩子,用于记录内存分配信息。
Linux下面也有原理相同的方法——mtrace,http://en.wikipedia.org/wiki/Mtrace。方法相似,我这就不具体描述,参加给出的连接。这节我主要介绍一个很是强大的工具valgrind。以下图所示:
如上图所示知道:
==6118== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6118== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==6118== by 0x8048724: GetMemory(char*, int) (in /home/netsky/workspace/a.out)
==6118== by 0x804874E: main (in /home/netsky/workspace/a.out)
是在main中调用了GetMemory致使的内存泄漏,GetMemory中是调用了malloc致使泄漏了100字节的内存。
Things to notice:
• There is a lot of information in each error message; read it carefully.
• The 6118 is the process ID; it’s usually unimportant.
• The first line ("Heap Summary") tells you what kind of error it is.
• Below the first line is a stack trace telling you where the problem occurred. Stack traces can get quite large, and be
confusing, especially if you are using the C++ STL. Reading them from the bottom up can help.
• The code addresses (eg. 0x4024F20) are usually unimportant, but occasionally crucial for tracking down weirder
bugs.
The stack trace tells you where the leaked memory was allocated. Memcheck cannot tell you why the memory leaked,
unfortunately. (Ignore the "vg_replace_malloc.c", that’s an implementation detail.)
There are several kinds of leaks; the two most important categories are:
• "definitely lost": your program is leaking memory -- fix it!
• "probably lost": your program is leaking memory, unless you’re doing funny things with pointers (such as moving
them to point to the middle of a heap block)
Valgrind的使用请见手册http://valgrind.org/docs/manual/manual.html。
其实内存泄漏的缘由能够归纳为:调用了malloc/new等内存申请的操做,但缺乏了对应的free/delete,总之就是,malloc/new比free/delete的数量多。咱们在编程时须要注意这点,保证每一个malloc都有对应的free,每一个new都有对应的deleted!!!平时要养成这样一个好的习惯。
要避免内存泄漏能够总结为如下几点:
转自吴秦