Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473015
  • 博文数量: 58
  • 博客积分: 6047
  • 博客等级: 准将
  • 技术积分: 838
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-06 21:05
文章分类

全部博文(58)

文章存档

2009年(24)

2008年(34)

我的朋友

分类:

2009-01-21 22:54:35

众所周知,mdb和dtrace是Solaris上的两把利剑。mdb不仅可以事实观察内核中事实的运行状态,也可以对应用程序进行调试。坚持内存泄露可能对大部分开发人员都是一件头疼的事情,借助mdb的帮助,可以很方便的检查出程序的内存泄露。

在介绍之前,先介绍一下libmum,libmum是一个对内存进行检测的库,如果想使用mdb检查内存泄露,在程序运行时需要加载libmum库。

export UMEM_DEBUG=default
export UMEM_LOGGING=transaction
export LD_PRELOAD=libumem.so.1

下面这个程序有明星的内存泄露。申请的内存没有被释放掉。
使用ps -ef 找到运行程序的pid,使用gcore生成一个core文件,利用mdb打开core文件
bash-3.2# mdb core.237
Loading modules: [ libumem.so.1 ld.so.1 ]
> ::findleaks
CACHE     LEAKED   BUFCTL CALLER
0806a290       1 0807f200 libstdc++.so.6.0.3`_Znwj+0x29
0806a290      60 0807f2f0 libstdc++.so.6.0.3`_Znwj+0x29
------------------------------------------------------------------------
   Total      61 buffers, 976 bytes
>
可以看到libstdc++.so.6.0.3`_Znwj+0x29有60个leaked,根据bufctl的地址查看bufctl的信息。
> 0807f2f0::bufctl -v
            ADDR          BUFADDR        TIMESTAMP           THREAD
                            CACHE          LASTLOG         CONTENTS
         807f2f0          8079fa0     ad78129dfba5                1
                          806a290                0                0
                 libumem.so.1`umem_cache_alloc_debug+0x144
                 libumem.so.1`umem_cache_alloc+0x153
                 libumem.so.1`umem_alloc+0xcd
                 libumem.so.1`malloc+0x2a
                 libstdc++.so.6.0.3`_Znwj+0x29
                 main+0x27
                 _start+0x80


#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

class TestClass
{
        public:
        TestClass() {
                cout << "no-argument" <<endl;
        }
        TestClass(const char *name) {
                cout << "arg = " << name << endl;
        }
};
int main(int argc, char **argv)
{
        TestClass *t;
        TestClass *tt;
        while (1) {
                t = new TestClass();
                t = new TestClass("Hello.");
                tt = new TestClass("Goodbye.");
                delete(t);
                delete(tt);
                sleep(1);
        }
}

阅读(4704) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~