A method of locating memory leaks (Linux) __linux

Source: Internet
Author: User

This article is the Linux version of a method for locating memory leaks (Solaris), which uses GDB. Mainly introduces the example part. See "A method of locating memory leaks (Solaris)." Example:

To simulate a new failed program:

#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 compile and run this piece of code. Generate a core file

2 Open this core file with GDB:

GDB A.out Core

(GDB) Run

Starting program:/test/new_fail/a.out

Terminate called after throwing a 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]

... Slightly

total:11468k 1048K 684K 180K 0K

360K writable-private, 11108K readonly-private, 0K shared, and 1048K referenced

You can see that the starting address of the heap space is 0x0000000000602000, a total of 132K bytes, that is, 132*1024=135168 bytes.

3 because it is a 64-bit application, the pointer occupies 8 bytes. So the number of pointers that need to be traversed is 135168/8=16896.

4 Output The results to the log file Gdb.txt:

(gdb) Set height 0

(GDB) Set logging on

Copying output to Gdb.txt.

(GDB) x/16896a 0x0000000000602000

Content of Gdb.txt:

0x602000:0x0 0x21

0x602010:0x400b30 <_ZTV3ABC+16> 0x0

0x602020:0x0 0x21

0x602030:0x400b30 <_ZTV3ABC+16> 0x0

....

5) Filtration Gdb.txt:

awk ' {print $ '/n ' $} ' gdb.txt|c++filt|grep Vtable>gdb_vtable.txt

The contents of the Gdb_vtable.txt are:

<vtable for Abc+16>

<vtable for Abc+16>

<vtable for Abc+16>

<vtable for Abc+16>

....

6 Import gdb_vtable.txt content into SQL Server (if you don't have a lot of records, you can replace it with Excel). The table name is Gdb_vtable, and the first column Col001 is a symbol. Sum it up by grouping:

Select Col001, COUNT (1) Quantity from gdb_vtable

GROUP BY Col001

ORDER BY Quantity desc

The results are:

Col001 Quantity

<vtable for Abc+16> 1000

<vtable for std::bad_alloc@ @GLIBCXX_3 .4+16> 1

There are 1000 ABC in the core, traversing the code using ABC to know that there is a leak.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.