一種定位記憶體泄露的方法(Linux)__Linux

來源:互聯網
上載者:User

本文是《一種定位記憶體泄露的方法(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的代碼,可知存在泄漏。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.