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.
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.