Use Linux's Mtrace command to locate memory leaks (Leak)

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 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
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. Setenv ("Malloc_trace", "Taoge.log", "1");
  5. Mtrace ();
  6. int *p = (int *) malloc (2 * sizeof (int));
  7. return 0;
  8. }

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)

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.