How to check memory leakage in Linux

Source: Internet
Author: User
Tags perl script
Article Title: How to check memory leakage in Linux. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems and open-source systems. call void mtrace (void) (in the mcheck. in h? 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. Some Memory code may not run until muntrace is released.
 
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.
 
Additional instructions
 
1. mtrace and muntrace can be placed into the signal processing function (USR1, USR2) to dynamically check and control memory leakage.
 
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:
 
In addition to glibc, you can also try some special programs.
 
It is strange that redhat 9 does not contain the mtrace perl script, so we have to download the gcc source code and compile it.
 
Wget -- passive-ftp linux/redhat/9 "> ftp://rpmfind.net/linux/redhat/9 ...... -2.3.2-11.9.src.rpm
 
Rpm-ivh glibc *. src. rpm
 
Cd/usr/src/redhat/SPECS/
 
Rpmbuild-ba glibc-9.spec
 
Cd/var/tmp/glibc-2.3.2-root/usr/bin/
 
Cp mtrace/usr/bin/
 
The debugging method is as follows:
 
Vi a. c
 
1 # include
 
2
 
3 int main ()
 
4 {
 
5 mtrace ();
 
6 malloc (10 );
 
7 malloc (16 );
 
8 return 0;
 
9}
 
$ Gcc-g a. 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.
 
Related Article

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.