When it comes to memory leaks, most programs ape smell the color change. Yes, memory leaks are very easy to introduce. But it's very difficult to locate. Take your My phone as an example (if not often shut down). If you leak some memory every day, you will find your phone is good for the first one weeks. When memory leaks accumulate to a certain extent, it is all sorts of cards are dead. System exception, the last panic. Had to restart.
Suppose to engage in development. Encounter memory leak problem, then hehe. You may have to spend days to reproduce the problem (leak accumulation), then take days to locate the problem and change the problem, and then spend a few days to verify the problem, and. There is a good chance that the process will not be changed again.
It's really tough.
I personally feel that on the memory leak issue. Proactive prevention is much more cost effective than passive positioning. But no matter how you prevent it, there is always a time to lose the chain, so sometimes you have to passively locate the memory leak.
In this article. I'm not talking about the location of the phone's memory leak, just a practical Linux small command: mtrace (memory trace), which can be used to assist in locating the leak. Development, should have heard more or less mtrace.
Below, let's take a look at the program:
#include <stdio.h>int main () { setenv ("Malloc_trace", "Taoge.log", "1"); Mtrace (); int *p = (int *) malloc (2 * sizeof (int)); return 0;}
Some friends have to say, a glance can see the memory leaks ah. But. How can you only rely on the naked eye when the program is big? Well, mtrace should be out.
Compilation: Gcc-g-ddebug test.c (be aware that the-G must not be missed.) Otherwise, although the memory leak can finally be located, it cannot be found in the first line of code. Because there is no debug macro control in my code,-ddebug can be omitted at compile time. Therefore, directly written gcc-g test.c can be)
Execution:./a.out
Location: Mtrace a.out Taoge.log
Results:
You can see that there is a memory leak and that the number of lines of code is correctly located.
We think about the principle of the Mtrace function/command, in fact, is very easy, nothing more than to record each pair of Malloc/free call situation, in this sense, mtrace replaced part of our eyes, staring closely at Malloc/free, So you can see the leak or not leak ah.
explain. My Linux is not installed Mtrace command, so can not debug, with the help of netizens Jukay, I only contact Shiyanlou this excellent online tool, address is: https://www.shiyanlou.com/, we do not need to register. You can log in directly with QQ. The above process is done in the Shiyanlou. Friends who don't have a Linux environment. You'll be able to play on it later, and stop saying there's no Linux environment. Thanks again for Jukay for introducing such an excellent online tool.
OK, this article first written here, perhaps will continue to introduce some Linux-related basic debugging tools and methods.
Use Linux's Mtrace command to locate memory leaks (Leak)