“ 目录:RHCA+ -> 12 系统监控 -> 33 valgrind命令(检测内存泄漏) ”
内存调试和性能分析工具,常用于检测内存泄漏、非法内存访问、使用未初始化变量等;语法格式:valgrind [参数] 被检测命令
此命令会降低被检测命令的运行速度10-50倍,所以只用于调试
参数 | 作用 |
--tool | 指定分析工具 memcheck:跟踪非法内存访问 cachegrind:分析缓存、性能 helgrind:检测多线程竞争、锁等问题 |
--log-file | 将输出重定向到文件 |
--v | 查看详细信息 |
--vv | 查看更详细的信息 |
--track-fds=yes | 跟踪文件描述符泄漏 |
--suppressions=文件名 | 忽略指定规则文件中定义的错误 |
--leak-check=full | 查看详细的内存泄漏信息,包括未释放的内存位置 |
--track-origins=yes | 查看未初始化变量的来源,比如具体代码行 |
回显结果
definitely lost:内存泄漏
possibly lost:可能泄漏,例:指针未置空
still reachable:程序结束时仍可达的内存,通常是全局变量
一、安装
[root@wuzuniao ~]# yum -y install valgrind #安装调试软件
二、使用
[root@wuzuniao ~]# valgrind --leak-check=full ls #检查是否有内存泄露
==3364== Memcheck, a memory error detector
==3364== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3364== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==3364== Command: ls
==3364==
anaconda-ks.cfg a.txt cpuset Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
==3364==
==3364== HEAP SUMMARY:
==3364== in use at exit: 22,693 bytes in 18 blocks
==3364== total heap usage: 52 allocs, 34 frees, 59,240 bytes allocated
==3364==
==3364== LEAK SUMMARY:
==3364== definitely lost: 0 bytes in 0 blocks
==3364== indirectly lost: 0 bytes in 0 blocks
==3364== possibly lost: 0 bytes in 0 blocks
==3364== still reachable: 22,693 bytes in 18 blocks
==3364== suppressed: 0 bytes in 0 blocks
==3364== Reachable blocks (those to which a pointer was found) are not shown.
==3364== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==3364==
==3364== For counts of detected and suppressed errors, rerun with: -v
==3364== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 3)
[root@wuzuniao ~]#