Valgrind centos

Valgrind是一款強大的可以用來構建動態分析的工具。可以使用它來進行內存泄漏的檢測。


Valgrind安裝

現在來看下Valgrind的安裝(CentOS下)

首先可以執行如下命令進行Valgrind包的下載:

curl -O  https://sourceware.org/ftp/valgrind/valgrind-3.14.0.tar.bz2

解壓縮:

tar xvf valgrind-3.14.0.tar.bz2

進入解壓完的目錄

cd valgrind-3.14.0/

執行如下命令:

./autogen.sh

如果出現如下錯誤:

running: aclocal
./autogen.sh: line 6: aclocal: command not found
error: while running 'aclocal'

缺少依賴,執行如下命令進行安裝即可:

yum install automake

安裝完後再次執行./autogen.sh即可

接着執行如下命令:

./configure

如果出現如下錯誤:

是gcc沒安裝,可通過執行如下命令安裝解決:

yum install gcc

安裝完後再執行下./configure 即可。

然後執行如下兩個命令完成安裝:

make

make install 

可以執行下如下命令查看下Valgrind的版本,如果能正確返回說明安裝成功

[root@localhost valgrind-3.14.0]# valgrind --version
valgrind-3.14.0

Valgrind簡單使用

現在來簡單介紹下如何使用Valgrind來進行內存泄漏的檢測。

進入到你被測程序的可執行文件所在路徑,假設被測程序的可執行文件爲xxx,那麼執行如下命令即可啓動Valgrind並同時啓動被測試程序進行檢測

valgrind --tool=memcheck --leak-check=full --error-limit=no --num-callers=50 --log-file=/home/aaron/valgrind_report.log ./xxxx

現在來簡要介紹下上面命令的幾個配置項的含義:

--tool=memcheck :使用內存檢測的工具

--leak-check=full :有設置這個選項時,當被測程序結束時查找內存泄露。設置爲summary,Valgrind會統計有多少內存泄露發生,若設置爲full、yes,Valgrind會給出每一個獨立的泄露的詳細信息

--error-limit=no:如果錯誤太多要停止顯示新的錯誤的選項

--num-callers=50:這個值默認是12,最高是50。表示顯示多少層的堆棧,設置越高會使Valgrind運行越慢而且使用更多的內存,但是在嵌套調用層次比較高的程序中非常實用

--log-file=/home/aaron/valgrind_report.log: 指定檢測結果報告的存放路徑和文件名

./xxxx: 執行被測可執行文件

執行完上面的命令後就可以進行一些測試場景的執行,待要結束內存泄漏檢測的時候在執行Valgrind命令的地方執行ctlr+c就可以終止Valgrind執行了,然後去查看指定路徑產生的報告,報告會有類似如下的信息:

==18426== LEAK SUMMARY:
==18426==    definitely lost: 0 bytes in 0 blocks
==18426==    indirectly lost: 0 bytes in 0 blocks
==18426==      possibly lost: 7,296 bytes in 12 blocks
==18426==    still reachable: 112,897 bytes in 3,365 blocks
==18426==                       of which reachable via heuristic:
==18426==                         stdstring          : 846 bytes in 22 blocks
==18426==         suppressed: 0 bytes in 0 blocks
==18426== Reachable blocks (those to which a pointer was found) are not shown.
==18426== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==18426== 
==18426== For counts of detected and suppressed errors, rerun with: -v
==18426== Use --track-origins=yes to see where uninitialised values come from
==18426== ERROR SUMMARY: 252 errors from 14 contexts (suppressed: 6 from 4)

重點關注 definitely lost(代表有確定的內存泄露)和indirectly lost(有間接的內存泄漏)

possibly lost表示可能有,這個可以根據實際情況讓研發看下是否需要解決。

這幾個的官方解釋如下:

"definitely lost" means your program is leaking memory -- fix those leaks!

"indirectly lost" means your program is leaking memory in a pointer-based structure. (E.g. if the root node of a binary tree is "definitely lost", all the children will be "indirectly lost".) If you fix the "definitely lost" leaks, the "indirectly lost" leaks should go away.

"possibly lost" means your program is leaking memory, unless you're doing unusual things with pointers that could cause them to point into the middle of an allocated block; see the user manual for some possible causes. Use --show-possibly-lost=no if you don't want to see these reports.

"still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.

"suppressed" means that a leak error has been suppressed. There are some suppressions in the default suppression files. You can ignore suppressed errors.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章