您好,登錄后才能下訂單哦!
這篇文章主要介紹“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 <stdio.h> #include <stdlib.h> 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 <stdio.h> #include <stdlib.h> 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虛擬內存,內存泄露和檢測舉例分析”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。