免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3395 | 回复: 0
打印 上一主题 下一主题

如何在solaris上检测内存泄露 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-21 22:54 |只看该作者 |倒序浏览
众所周知,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);
        }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/58855/showart_1806638.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP