linux虚拟内存,内存泄露和检测举例分析
这篇文章主要介绍“linux虚拟内存,内存泄露和检测举例分析”,在日常操作中,相信很多人在linux虚拟内存,内存泄露和检测举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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 Library 7. MEMWATCH: 由Johan Lindh编写,是一个开放源代码C语言内存错误检测工具,主要是通过gcc的precessor来进行 8. KCachegrind: A visualization tool for the 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)
到此,关于“linux虚拟内存,内存泄露和检测举例分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
名称栏目:linux虚拟内存,内存泄露和检测举例分析
本文链接:http://scyanting.com/article/poosgh.html