When it comes to memory leaks, most programmers smell the color change. Yes, memory leaks are easy to introduce, but difficult to locate. Take your My phone as an example (assuming not often shut down), if you leak some memory every day, then the beginning of one weeks, you will find that the mobile phone good, when the memory leaks accumulate to a certain extent, it is all kinds of card dead, the system is abnormal, the last crash, have to restart.
If the development, encounter memory leak problem, then hehe. You may have to spend a few days to reproduce the problem (leak accumulation), then need to spend several days to locate the problem and modify the problem, and then spend a few days to verify the problem, and, it is likely to not be changed at once, the process has to cycle again. It's really tough.
I personally think that in memory leaks, proactive prevention is more cost-effective than passive positioning, but no matter how you prevent it, there is always a time to lose the chain, so sometimes have to passively locate the memory leak.
In this article, I'm not talking about the location of the phone memory leak, just a useful linux little 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:
[CPP]View PlainCopy
- #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 last to locate a memory leak, but can not find in the code of the first few lines. Because I do not have debug macro control in the code, so compile,-ddebug can be omitted, therefore, directly written gcc-g test.c can be)
Run:./a.out
Location: Mtrace a.out Taoge.log
Results:
As you can see, there is a memory leak, and the number of lines of code is correctly located.
We think about the principle of mtrace function/command, in fact, is also very simple, is nothing more than a record of each pair of Malloc/free call situation, in this sense, mtrace replaced part of our eyes, staring closely at Malloc/free, so can see leaks or not leak ah.
explain, my Linux does not install Mtrace command, so can not debug, with the help of Netizen Jukay, I only contact Shiyanlou this excellent online tool, address is: https://www.shiyanlou.com/, We do not need to register, directly with QQ login can, the above process is done in the Shiyanlou. Friends who don't have a Linux environment will be able to play on it later, no more reason to say there is no Linux environment. Thanks again for Jukay for introducing such an excellent online tool.
OK, this article is first written here, the following will continue to introduce some Linux-related basic debugging tools and methods.
Use Linux's Mtrace command to locate memory leaks (Leak)