Linux环境内存泄漏工具vargrind

发布时间:2017-2-26 9:23:47编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Linux环境内存泄漏工具vargrind",主要涉及到Linux环境内存泄漏工具vargrind方面的内容,对于Linux环境内存泄漏工具vargrind感兴趣的同学可以参考一下。

今天又温习了一下linux下检测内存泄漏工具vargrind的使用:

1. 从网上看,大家都用的是valgrind工具集,valgrind包含了几种工具:

  (1) memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。

  (2) callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能。

  (3) cachegrind:分析CPU的cache命中率、丢失率,用于进行代码优化。

  (4) helgrind:用于检查多线程程序的竞态条件。

  (5) massif:堆栈分析器,指示程序中使用了多少堆内存等信息。


2. 只说memcheck这个工具,它负责检测内存问题:

  (1) 使用未初始化的内存,这块内存的值是随机的。

  (2) 使用未指向有效内存的指针,即空指针或者野指针。

  (3) 内存越界方法。

  (4) 内存复制区域的重叠。

使用未初始化的内存 (Use of uninitialised memory)
使用已经释放了的内存 (Reading/writing memory after it has been free’d)
使用超过malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
重复free


3. 使用方法:

valgrind 选项--tool=name,name指定使用哪个工具,比如memcheck等。

Memcheck选项:

-leak-check=no|summary|full 要求对leak给出详细信息,即内存泄漏的详细信息。


4. 例子说明1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *p;
    *p = 'a';

    return 0;
}

使用分配内存且未初始化的指针p,检测如下:

[[email protected] valgrind]$ valgrind --tool=memcheck ./mem-leak
==1841== Memcheck, a memory error detector
==1841== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1841== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1841== Command: ./mem-leak
==1841==
==1841== Use of uninitialised value of size 4
==1841==    at 0x8048404: main (mem-leak.c:8)
==1841==
==1841==
==1841== HEAP SUMMARY:
==1841==     in use at exit: 0 bytes in 0 blocks
==1841==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==1841==
==1841== All heap blocks were freed -- no leaks are possible
==1841==
==1841== For counts of detected and suppressed errors, rerun with: -v
==1841== Use --track-origins=yes to see where uninitialised values come from
==1841== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

以上是valgrind命令检测结果的输出,

==1841== Use of uninitialised value of size 4  指出使用未初始化的值
==1841==    at 0x8048404: main (mem-leak.c:8) 指出代码位置行数

例子说明2:分配内存,不进行释放。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *p = malloc(sizeof(char) * 100);

    return 0;
}

[[email protected] valgrind]$ valgrind --tool=memcheck --leak-check=full ./mem-leak2
==1901== Memcheck, a memory error detector
==1901== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1901== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1901== Command: ./mem-leak2
==1901==
==1901==
==1901== HEAP SUMMARY:                                                                                       堆的统计信息:
==1901==     in use at exit: 100 bytes in 1 blocks
==1901==   total heap usage: 1 allocs, 0 frees, 100 bytes allocated                  堆heap上分配了一次内存,100字节
==1901==
==1901== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1            指出100字节丢失即没有释放
==1901==    at 0x402A2C2: malloc (vg_replace_malloc.c:299)
==1901==    by 0x8048445: main (mem-leak2.c:7)                                                mem-leak2.c的第7行发生内存泄漏
==1901==
==1901== LEAK SUMMARY:                                                                                       leak-check选项的统计信息:
==1901==    definitely lost: 100 bytes in 1 blocks                                                   100字节绝对丢失
==1901==    indirectly lost: 0 bytes in 0 blocks
==1901==      possibly lost: 0 bytes in 0 blocks
==1901==    still reachable: 0 bytes in 0 blocks
==1901==         suppressed: 0 bytes in 0 blocks
==1901==
==1901== For counts of detected and suppressed errors, rerun with: -v
==1901== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


以后工作中慢慢用吧。




上一篇:Gson关于对内部类的序列化
下一篇:Unity下开发VR应用

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款