千家信息网

linux虚拟内存,内存泄露和检测举例分析

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍"linux虚拟内存,内存泄露和检测举例分析",在日常操作中,相信很多人在linux虚拟内存,内存泄露和检测举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对
千家信息网最后更新 2026年11月03日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.bz2cd valgrind-3.4.1/./configure --prefix /home/zhenghan.zh/valgrindmakemake 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虚拟内存,内存泄露和检测举例分析"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

内存 检测 分析 代码 程序 工具 问题 性能 学习 应用 编译 错误 静态 C++ 帮助 运行 产品 功能 动态 应用程序 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 深圳市人人互联网科技有限公司 新华互联网科技学校 互联网属科技 涨乐互联网科技 艾特科技互联网 百单互联网科技有限公司 天津摩尔互联网科技 互联网 新科技 多迪互联网科技有限公司 中国互联网金融科技基金 恒银科技与互联网金融 苏州铭阳互联网科技有限公司 国大互联网教育科技有限公司工作 互联网科技人工智能实验室 卓翼科技产业互联网合作 中科明德北京互联网科技 上海创道互联网络科技有限公司 普陀区互联网教育科技管理办法 互联网科技企业商标注意事项 互联网金融金融科技 苏州找大状互联网科技有限公司 互联网科技公司有哪些 千方科技属于互联网企业吗 高科技互联网套路 上海钢联属于互联网科技股吗 梅州GM互联网络科技有限公司 上海同邻互联网科技有限公司规模 互联网的科技体现在哪里 互联网科技企业文化 如何强化互联网金融科技
0