一种定位内存泄露的方法(Linux)
2015-07-10 10:42
543 查看
http://www.cppblog.com/sleepwom/archive/2012/03/26/168965.html
分类:
C/C++Linux 2010-05-16 18:35
612人阅读 评论
(0) 收藏
举报
目的:
本文是《一种定位内存泄露的方法( Solaris )》对应的 Linux 版本,调试器使用 gdb 。主要介绍实例部分。其他请见《一种定位内存泄露的方法( Solaris )》。
实例:
模拟 new 失败的程序:
#include <stdexcept>
class ABC
{
public:
virtual ~ABC(){}
int i;
int j;
};
void f()
{
for (int i = 0; i < 1000; ++i)
{
ABC* p = new ABC;
}
throw std::bad_alloc();
}
int main()
{
f();
return 0;
}
1) 编译运行此段代码。产生一个 core 文件
2) 用 gdb 打开这个 core 文件:
gdb a.out core
(gdb)run
Starting program: /test/new_fail/a.out
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Program received signal SIGABRT, Aborted.
0x00007ffff733f645 in raise () from /lib64/libc.so.6
(gdb)info proc
process 10683
cmdline = '/test/new_fail/a.out'
cwd = '/test/new_fail'
exe = '/test/new_fail/a.out'
(gdb) shell pmap 10683
10683: a.out
START SIZE RSS PSS DIRTY SWAP PERM MAPPING
0000000000400000 4K 4K 4K 0K 0K r-xp /test/new_fail/a.out
0000000000600000 4K 4K 4K 4K 0K r--p /test/new_fail/a.out
0000000000601000 4K 4K 4K 4K 0K rw-p /test/new_fail/a.out
0000000000602000 132K 32K 32K 32K 0K rw-p [heap]
… (略)
Total: 11468K 1048K 684K 180K 0K
360K writable-private, 11108K readonly-private, 0K shared, and 1048K referenced
可以看到 heap 空间的起始地址是 0x0000000000602000 ,共 132K 字节,即 132*1024=135168 字节。
3) 因为是 64 位应用程序,所以指针占 8 字节。所以需要遍历的指针个数为 135168/8=16896 。
4) 将结果输出到日志文件 gdb.txt 中:
(gdb) set height 0
(gdb) set logging on
Copying output to gdb.txt.
(gdb) x/16896a 0x0000000000602000
gdb.txt 的内容:
0x602000: 0x0 0x21
0x602010: 0x400b30 <_ZTV3ABC+16> 0x0
0x602020: 0x0 0x21
0x602030: 0x400b30 <_ZTV3ABC+16> 0x0
….
5) 过滤 gdb.txt :
awk '{print $2"/n"$3}' gdb.txt|c++filt|grep vtable>gdb_vtable.txt
gdb_vtable.txt 的内容为:
<vtable for ABC+16>
<vtable for ABC+16>
<vtable for ABC+16>
<vtable for ABC+16>
….
6) 将 gdb_vtable.txt 的内容导入到 SQLServer 中(如果记录不多,可以用 Excel 代替)。表名为 gdb_vtable ,第一列 Col001 为符号。对其分组求和:
select Col001, count(1) quantity from gdb_vtable
group by Col001
order by quantity desc
结果为:
Col001 quantity
<vtable for ABC+16> 1000
<vtable for std::bad_alloc@@GLIBCXX_3.4+16> 1
可知 core 里有 1000 个 ABC ,遍历使用 ABC 的代码,可知存在泄漏。
分类:
C/C++Linux 2010-05-16 18:35
612人阅读 评论
(0) 收藏
举报
目的:
本文是《一种定位内存泄露的方法( Solaris )》对应的 Linux 版本,调试器使用 gdb 。主要介绍实例部分。其他请见《一种定位内存泄露的方法( Solaris )》。实例:
模拟 new 失败的程序:#include <stdexcept>
class ABC
{
public:
virtual ~ABC(){}
int i;
int j;
};
void f()
{
for (int i = 0; i < 1000; ++i)
{
ABC* p = new ABC;
}
throw std::bad_alloc();
}
int main()
{
f();
return 0;
}
1) 编译运行此段代码。产生一个 core 文件
2) 用 gdb 打开这个 core 文件:
gdb a.out core
(gdb)run
Starting program: /test/new_fail/a.out
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Program received signal SIGABRT, Aborted.
0x00007ffff733f645 in raise () from /lib64/libc.so.6
(gdb)info proc
process 10683
cmdline = '/test/new_fail/a.out'
cwd = '/test/new_fail'
exe = '/test/new_fail/a.out'
(gdb) shell pmap 10683
10683: a.out
START SIZE RSS PSS DIRTY SWAP PERM MAPPING
0000000000400000 4K 4K 4K 0K 0K r-xp /test/new_fail/a.out
0000000000600000 4K 4K 4K 4K 0K r--p /test/new_fail/a.out
0000000000601000 4K 4K 4K 4K 0K rw-p /test/new_fail/a.out
0000000000602000 132K 32K 32K 32K 0K rw-p [heap]
… (略)
Total: 11468K 1048K 684K 180K 0K
360K writable-private, 11108K readonly-private, 0K shared, and 1048K referenced
可以看到 heap 空间的起始地址是 0x0000000000602000 ,共 132K 字节,即 132*1024=135168 字节。
3) 因为是 64 位应用程序,所以指针占 8 字节。所以需要遍历的指针个数为 135168/8=16896 。
4) 将结果输出到日志文件 gdb.txt 中:
(gdb) set height 0
(gdb) set logging on
Copying output to gdb.txt.
(gdb) x/16896a 0x0000000000602000
gdb.txt 的内容:
0x602000: 0x0 0x21
0x602010: 0x400b30 <_ZTV3ABC+16> 0x0
0x602020: 0x0 0x21
0x602030: 0x400b30 <_ZTV3ABC+16> 0x0
….
5) 过滤 gdb.txt :
awk '{print $2"/n"$3}' gdb.txt|c++filt|grep vtable>gdb_vtable.txt
gdb_vtable.txt 的内容为:
<vtable for ABC+16>
<vtable for ABC+16>
<vtable for ABC+16>
<vtable for ABC+16>
….
6) 将 gdb_vtable.txt 的内容导入到 SQLServer 中(如果记录不多,可以用 Excel 代替)。表名为 gdb_vtable ,第一列 Col001 为符号。对其分组求和:
select Col001, count(1) quantity from gdb_vtable
group by Col001
order by quantity desc
结果为:
Col001 quantity
<vtable for ABC+16> 1000
<vtable for std::bad_alloc@@GLIBCXX_3.4+16> 1
可知 core 里有 1000 个 ABC ,遍历使用 ABC 的代码,可知存在泄漏。
相关文章推荐
- linux内存
- Centos 安装R 集成 Hadoop、RHive 配置安装手册
- Centos安装ImageMagick 与 imagick for php详解
- Linux下内存统计和内存泄露类问题的定位方法
- linux系统怎么重启网卡?linux重启网卡的三种教程
- Linux下halt,poweroff,reboot,shutdown命令的区别
- Linux 常用命令
- Linux FTP服务器-VSFTPD虚拟用户配置
- linux(centos)下通过yum安装mysql5.6
- MACOS,LINUX,IOS上可用的毫秒级精度时间获取
- 为arm平台构建linux内核树
- linux重要日志文件
- linux清除缓存
- Linux中查找最耗性能的JAVA代码
- 在 RHEL/CentOS 7.0 中安装 LAMP
- Linux的Cgroup<实例详解>
- Linux下暴力破解工具Hydra详解
- linux 内存管理
- Linux 安装
- Linux菜鸟学习笔记