Use Linux's Mtrace command to locate memory leaks (Memory leak) __linux

Source: Internet
Author: User

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 mobile phone as an example (if not often shut down), if some memory leakage every day, then the beginning of the one weeks, you will find the phone well, when the memory leak accumulated to a certain extent, it is a variety of cards die, system anomalies, the last crash, had to restart.

If develop, encounter memory leak problem, that is hehe. You may have to spend a few days to reproduce the problem (the leak accumulates), then take a few days to locate and modify the problem, and then spend a few days validating the problem, and there is a good chance that the process will not be changed again. It's really hard.

I personally think that in the memory leak problem, proactive prevention than passive positioning is much more cost-effective, but no matter how you prevent, there is always a chain, so, sometimes have to passively locate the memory leak.


In this article, let's not talk about the location of the phone memory leak problem, just a useful linux small command: mtrace (memory trace), which can be used to help locate memory leaks. Development, should have heard more or less mtrace.

Next, 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 to say, at a glance can see the memory leak AH. However, when the program is big, how can only rely on the naked eye. Well, it's time to mtrace.


Compile: Gcc-g-ddebug test.c (be sure to note that-G must not be missed, otherwise, although the final positioning to the memory leak, but can not find in the first few lines of code.) Because my code does not have debug macro control, so compile,-ddebug can be omitted, so, directly written gcc-g test.c can be

Run:./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 positioned.


We think about the principle of mtrace function/command, in fact, is very simple, nothing more than a record of each pair of malloc/free of the call, in this sense, mtrace replaced part of our eyes, tightly stared at the malloc/free, so can see the leak or not leak ah.



explain that, my Linux does not install Mtrace command, so can not debug, with the help of netizens Jukay, I came into contact with 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. No Linux environment friends, you can play on this later, no reason to say there is no Linux environment. Thank Jukay again for introducing such an excellent online tool.


OK, here's what this article is about, and the following will continue to introduce some basic Linux-related debugging tools and methods.






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.