linux 虚拟内存 malloc,虚拟内存,内存泄露和检测(linux)

内存泄露

所谓的内存泄露,指的是不再使用的内存,没能得到释放,并且之后也没有机会释放。

内存占用过多,指的是大量申请内存,但是没有及时的释放内存,但是对于c++这类没有垃圾回收器的语言来说,相当于内存泄露。

内存泄露的检测分为静态检测和动态检测,即编译时检测和运行时检测。

静态检测,如pclint,pgrelief等代码静态检查工具,可以在编译时就提前检测到可能有问题的代码。

动态检测,如下:

内存泄露的常用检测工具(运行时检测)

C/C++

1. Valgrind: Debugging and profiling Linux programs, aiming at programs written in C and C++

2. ccmalloc: Linux和Solaris下对C和C++程序的简单的使用内存泄漏和malloc调试库3. LeakTracer: Linux、Solaris和HP-UX下跟踪和分析C++程序中的内存泄漏4. Electric Fence: Linux分发版中由Bruce Perens编写的malloc()调试库5. Leaky: Linux下检测内存泄漏的程序6. Dmalloc: Debug Malloc Library7. MEMWATCH: 由Johan Lindh编写,是一个开放源代码C语言内存错误检测工具,主要是通过gcc的precessor来进行8. KCachegrind: A visualization tool forthe profiling data generated by Cachegrind and Calltree

Java1. Memory Analyzer: 是一款开源的JAVA内存分析软件,查找内存泄漏,能容易找到大块内存并验证谁在一直占用它,它是基于Eclipse RCP(Rich Client Platform),可以下载RCP的独立版本或者Eclipse的插件2. JProbe: 分析Java的内存泄漏3. JProfiler: 一个全功能的Java剖析工具,专用于分析J2SE和J2EE应用程序。它把CPU、执行绪和内存的剖析组合在一个强大的应用中,GUI可以找到效能瓶颈、抓出内存泄漏、并解决执行绪的问题4. JRockit: 用来诊断Java内存泄漏并指出根本原因,专门针对Intel平台并得到优化,能在Intel硬件上获得最高的性能5. YourKit .NET &Java Profiling: 业界领先的Java和.NET程序性能分析工具6. AutomatedQA: AutomatedQA的获奖产品performance profiling和memory debugging工具集的下一代替换产品,支持Microsoft, Borland, Intel, Compaq 和 GNU编译器。可以为.NET和Windows程序生成全面细致的报告,从而帮助您轻松隔离并排除代码中含有的性能问题和内存/资源泄露问题。支持.Net 1.0,1.1,2.0,3.0和Windows 32/64位应用程序7. Compuware DevPartner Java Edition: 包含Java内存检测,代码覆盖率测试,代码性能测试,线程死锁,分布式应用等几大功能模块

Valgrind的使用-编译安装(建议去官网下载最新版)

wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2或者官网https://www.valgrind.org/downloads/current.html下载最新版.

tar xvf valgrind-3.4.1.tar.bz2

cd valgrind-3.4.1/./configure --prefix /home/zhenghan.zh/valgrind

make

make install

Valgrind的使用-运行时检测

一段没有错误的代码

#include

#include

void fun()

{

int *p = (int*)malloc(10*sizeof(int));

p[9] = 0;

free(p);

}

int main()

{

fun();

return 0;

}

gcc -g -O0 1.c

valgrind --tool=memcheck --leak-check=full ./a.out

结果如下:

wanglc@wanglc-VirtualBox:~/cpp$ valgrind --tool=memcheck --leak-check=full ./a.out

==29920== Memcheck, a memory error detector

==29920== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.

==29920== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info

==29920== Command: ./a.out

==29920==

==29920==

==29920== HEAP SUMMARY:

==29920==     in use at exit: 0 bytes in 0 blocks

==29920==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated

==29920==

==29920== All heap blocks were freed -- no leaks are possible

==29920==

==29920== For lists of detected and suppressed errors, rerun with: -s

==29920== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

一段内存错误的代码.

#include

#include

void fun()

{

int *p = (int*)malloc(10*sizeof(int));

p[10] = 0;

}

int main()

{

fun();

return 0;

}

gcc -g -O0 1.c

valgrind --tool=memcheck --leak-check=full ./a.out

结果如下(未释放和数组越界都被检测出来了):

wanglc@wanglc-VirtualBox:~/cpp$ valgrind --tool=memcheck --leak-check=full ./a.out

==30261== Memcheck, a memory error detector

==30261== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.

==30261== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info

==30261== Command: ./a.out

==30261==

==30261== Invalid write of size 4

==30261==    at 0x108668: fun (1.c:7)

==30261==    by 0x10867E: main (1.c:12)

==30261==  Address 0x522e068 is 0 bytes after a block of size 40 alloc'd

==30261==    at 0x4C2FECB: malloc (vg_replace_malloc.c:307)

==30261==    by 0x10865B: fun (1.c:6)

==30261==    by 0x10867E: main (1.c:12)

==30261==

==30261==

==30261== HEAP SUMMARY:

==30261==     in use at exit: 40 bytes in 1 blocks

==30261==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated

==30261==

==30261== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1

==30261==    at 0x4C2FECB: malloc (vg_replace_malloc.c:307)

==30261==    by 0x10865B: fun (1.c:6)

==30261==    by 0x10867E: main (1.c:12)

==30261==

==30261== LEAK SUMMARY:

==30261==    definitely lost: 40 bytes in 1 blocks

==30261==    indirectly lost: 0 bytes in 0 blocks

==30261==      possibly lost: 0 bytes in 0 blocks

==30261==    still reachable: 0 bytes in 0 blocks

==30261==         suppressed: 0 bytes in 0 blocks

==30261==

==30261== For lists of detected and suppressed errors, rerun with: -s

==30261== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

以上

-----------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值