Mtrace check memory leakage

Source: Internet
Author: User
Tags perl script

Memory leakage check method (for Linux)

If you want to read the original document, see "Allocation debugging" in glibc info"
Chapter 1 (execute info libc );
Glibc provides a method to check for memory leaks, provided that your program uses standard glibc functions.
Allocate memory (such as malloc, alloc ...):

1. Call void mtrace (void) (IN mcheck. h) at the beginning of the Code that requires memory leak check.
? With declarations). mtrace installs hooks for functions such as malloc to record memory allocation information.
Call void muntrace (void) at the end of the Code that requires memory leak check ).
Note: In general, do not call muntrace, but let the program end naturally, because there may be some
The memory code to be released must be run after muntrace.

2. Compile the checked code in debug mode (-G or-ggdb)

3. Set the environment variable malloc_trace to a file name, which contains memory allocation information.

4. Run the checked program until it is finished or muntrace is called.

5. Use the mtrace command to parse the memory allocation log file ($ malloc_trace)
(Mtrace Foo $ malloc_trace, where foo is the executible name)
In case of Memory leakage, mtrace will output distribution Leakage
Memory code location and allocated quantity.

Other things

1. You can add mtrace and muntrace to the signal processing function (usr1, usr2) to dynamically perform
Memory leakage check control.

2. mtrace is a Perl code. If you are interested in the conversion of symbolic addresses and code texts, you can
Read it.

3. Again, try not to use muntrace ()

For C ++ leak:

The method for checking Memory leakage is not provided by glibc;
You can also try some special programs, such:
Ccmalloc (http://www.inf.ethz.ch/personal/biere/projects/ccmalloc/ccmalloc-english.html)
Mpatrol (http://www.cbmamiga.demon.co.uk/mpatrol)
The functions of these two tools are quite good, and the program can be thoroughly checked.

Very strange, RedHat 9 does not contain mtrace Perl script, had to download the GCC Source Code Compiled wget -- passive-FTP ftp://rpmfind.net/linux/redhat/9/en/ OS /i386/SRPMS/glibc-2.3.2-11.9.src.rpmrpm-IVH glibc *. SRC. rpmcd/usr/src/RedHat/specs/rpmbuild-Ba glibc-9.spec CD/var/tmp/glibc-2.3.2-root/usr/bin/CP mtrace/usr/bin/debugging method is as follows: Vim mtrace. c # Include <mcheck. h> </P> <p> int main () <br/> {<br/> mtrace (); <br/> malloc (10 ); <br/> malloc (16); <br/> return 0 <br/>}$ Gcc-G mtrace. C # Remember to compile the "-G" debugging Option
$ Export malloc_trace = A. Log
$./A. Out
$ Unset malloc_trace # Remember the unset variable after execution; otherwise, other commands may overwrite the log
$ Mtrace A. Out A. Log
Memory not freed:
-----------------
Address size caller
0x09b08378 0xa at/XXX/a. C: 6
0x09b08388 0x10 at/XXX/a. C: 7

As you can see, the location of the Code that does not release the dynamic space is displayed.

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.