Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83622
  • 博文数量: 14
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 153
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-13 11:26
文章分类

全部博文(14)

文章存档

2015年(2)

2014年(7)

2013年(5)

我的朋友

分类: LINUX

2013-10-17 17:43:01

参考文档1:http://blog.csdn.net/ithomer/article/details/6928318
软件下载地址:http://valgrind.org/downloads/current.html

常见检测内存泄漏的工具:
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.

wget /> tar xvf valgrind-3.8.1.tar.bz2
cd valgrind-3.8.1/
./configure --prefix=/usr/local/valgrind
make
make install

安装glibc的debug info:
sudo apt-get install libc6-dbg


//test.c
#include
void f(void){
    int* x = malloc(10 * sizeof(int));
    x[10] = 0;  //问题1: 数组下标越界
}                   //问题2: 内存没有释放
int main(void){
    f();
    return 0;
}


gcc test.c -o test

valgrind --tool=memcheck --leak-check=full ./test


gprof这个常用的性能工具,用来性能调优很方便。
但是有个致命的缺点,不能处理动态链接库(dlopen()加载的)。
那遇到动态链接库怎么调优呢,用这个工具callgrind。
同样是valgrind工具集中的一个,使用也是同样方便。gcc带上-g参数,然后用callgrind运行!
编译运行:
gcc -o test -g test.c
valgrind --tool=callgrind ./test
callgrind运行的程序会非常的慢。
运行完之后文件夹下出现了callgrind.out.***便是日志文件。更massif一样,需要用工具转化:
callgrind_annotate callgrind.out.438
有没有更给力的图形前端呢?有,将callgrind.out.***用kcachegrind打开看看


利用 gprof2dot 和graphviz 图形化定位linux c/c++系统性能瓶颈
1 下载
下载gprof2dot.py
下载graphviz-2.18.tar.gz
 
2 安装
chmod +x gprof2dot.py
tar -xf graphviz-2.18.tar.gz
./configure
make
make install
 
3 编译代码
gcc -pg -g -o test test.c
 
4 执行代码生成gmon.out
./test
ll gmon.out
如果gprof ./test:
gmon.out file is missing call-graph data
则表示没有正确的编译选项或者只有一个main函数
 
5 生成图片
gprof ./test  | ./gprof2dot.py -n0 -e0 | dot -Tsvg -o output.svg
==
gprof ./test > tmp1
./gprof2dot.py -n0 -e0 tmp1 > tmp2
dot -Tsvg -o output.svg tmp2
 
gprof2dot默认是部分函数调用图,对性能影响不大的函数调用都不显示,例如上图中没有出现类的构造,析构函数,
如果想要显示全部的函数调用,可以 gprof2dot -n0 -e0 ,默认是n0.5即影响小于5%的函数就不显示了。
当然这样图片会很乱,因为显示内容很多,可以 gprof2dot -n0 -e0 -s 
-s表示不显示诸如模板,函数入口参数等等,使得函数名称显示更加精简

阅读(2000) | 评论(0) | 转发(0) |
0

上一篇:ffmpeg - tutorial01

下一篇:AVI封装

给主人留下些什么吧!~~