如何在solaris上檢測內存泄露

 

轉自:

http://www.vmman.net/2010/10/%E5%A6%82%E4%BD%95%E5%9C%A8solaris%E4%B8%8A%E6%A3%80%E6%B5%8B%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2/

在介紹之前,先介紹一下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

內存泄露的程序

?Download memleak.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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);
        }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章