original text :http://www.cnblogs.com/san-fu-su/p/5737984.html

C There is no garbage collection mechanism in it , Sometimes you apply for dynamic memory and forget to release it , This is awkward ( Your program plays the role of a robber , It's a good child to borrow and return ). When you want to find out where the memory leak is , Some put in a lot of code , I have a headache . not so bad GNU C Libraries provide some simple ways .

mtrace and muntrace Two functions can help us track dynamic memory usage . The premise is that we set up MALLOC_TRACE environment variable , To change the environment variable, we need to point to a writable regular file in our system . Doing this :

MALLOC_TRACE=/your/path/to/file.txt
export MALLOC_TRACE

mtrace Would be malloc,remalloc and free Install some special handlers. The usage of these functions will be recorded in the file .

muntrace Will uninstall the previously installed special handlers. That means the end of the dynamic memory trace .

Generally speaking, we are in main The beginning of the function calls mtrace,return Previous call muntrace.

The prototypes of these two functions are given below

#include<mecheck.h>
void mtrace(void)
void muntrace(void)

Using examples :

#include<mcheck.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
#ifdef DEBUG
mtrace();
#endif
int *a = NULL
a = malloc(sizeof(int));
// Here we don't call free function
return 0;
}

In the code above , We didn't call muntrace(), It is not recommended to use . The reason is that linux C It's not just your program that tracks dynamic memory problems in ,C Libraries also use . If you call muntrace(), That means C The library stops tracking dynamic memory .

If you use macro definitions DEBUG Compile the file , Execute the compiled executable , You'll find that file.txt There's something in it that we don't understand . This is for the machine . majority linux The distribution comes with mtrace command ( you 're right , The same name ). Use mtrace Commands convert machine-readable content into human readable content . Use the following command :

mtrace a.out file.txt

The result of the above code executing the command is as follows

Memory not freed:
-----------------------
Address Size Caller
0x092a6378 0x4 at /root/tmp.c:

Obviously No 9 Yes malloc function , We didn't call the corresponding free() Function to free memory .

【 turn 】Linux C More articles on dynamic memory leak tracing methods

  1. linuxC Dynamic memory leak tracing method

    C There is no garbage collection mechanism in it , Sometimes you apply for dynamic memory and forget to release it , This is awkward ( Your program plays the role of a robber , It's a good child to borrow and return ). When you want to find out where the memory leak is , Some put in a lot of code , I have a headache . not so bad GNU C The library provides some information ...

  2. How to be in linux Check for memory leaks under

    Previous articles apply Valgrind Find out Linux Program memory problems introduced in the use of Linux System tools valgrind Simple way to detect memory leaks , This paper implements a tool to detect memory leaks , Including the principle and implementation details . ...

  3. How to be in linux Check for memory leaks under ( turn )

    In this paper, from :http://www.ibm.com/developerworks/cn/linux/l-mleak/ This paper aims at linux Under the C++ This paper discusses the detection method of memory leak and its implementation . These include ...

  4. C++ Program memory leak detection method

    One . Preface stay Linux There is... On the platform valgrind Can be very convenient to help us locate memory leaks , because Linux In the field of development, most of the scenarios are server running , Plus its open source properties , Relatively speaking , Dealing with problems is easy to form “ Unified ” Standards for . And in the W ...

  5. C++ The program is in Windows Various ways to locate memory leaks on the platform , And compared their advantages and disadvantages

    One . Preface stay Linux There is... On the platform valgrind Can be very convenient to help us locate memory leaks , because Linux In the field of development, most of the scenarios are server running , Plus its open source properties , Relatively speaking , Dealing with problems is easy to form “ Unified ” Standards for . And in the W ...

  6. [ course ] Android Native Memory leak detection methods

    Reprint please indicate the source :https://www.cnblogs.com/zzcperf/p/9563389.html Android testing C/C++ The method of memory leak is becoming more and more simple , Here are some examples of detection in different scenarios C/ ...

  7. Linux Clean up memory and Cache Method /proc/sys/vm/drop_caches

    Linux Clean up memory and Cache Method /proc/sys/vm/drop_caches Frequent file access can lead to system failure Cache There's been a big increase in usage $ free -m total used free shared ...

  8. iOS AFNetworking Memory leak handling methods

    iOS AFN Memory leak handling methods Carefully, do you also find that AFN What about the memory leak . Here is a solution for you AFN Memory leak methods . Single case solution AFN Memory leak + (AFHTTPSessionManager ...

  9. VS2005 Memory leak detection methods [ Reprint ]

    One . Not MFC Programs can detect memory leaks in the following ways : 1. The program begins with the following definition : #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __ ...

Random recommendation

  1. asp.net Next simple Epplus export excel

    The referenced namespace using System.IO; using OfficeOpenXml; /// <summary> /// export excel /// </summary> / ...

  2. android socket For programming Bufferreader One of the lessons of reading failure

    Because my mobile phone needs to be opened with a notebook wifi, I always don't want to get out of bed when I need to turn off the computer or let the computer play songs when I am playing with my mobile phone in bed , So I wondered if I could use one APP, And then through the LAN to control the computer on the mobile phone ? Do as you say . I use it on my computer ...

  3. Hadoop distributed file system : Architecture and Design

    introduction Hadoop distributed file system (HDFS) Designed to run on universal hardware (commodity hardware) Distributed file system on . It has a lot in common with existing distributed file systems . But at the same time , It and other distributed file systems ...

  4. Talk about dependency management tools -- from Maven,Gradle To Go

    http://jolestar.com/dependency-management-tools-maven-gradle/

  5. tcp Agreement mss The understanding of the

    Introducing MSS Before that, we have to understand the following important concepts .<blockquote>MTU: Maxitum Transmission Unit Maximum transmission unit MSS: Maxitum Segmen ...

  6. POJ1423 - Big Number(Stirling The formula )

    The main idea of the topic seek N! How many are there Answer key Second kill directly with formula ... Code : #include<iostream> #include<cmath> using namespace std; # ...

  7. log4j Output information to mongodb

    Official website     http://log4mongo.org/display/PUB/Log4mongo+for+Java Save host information , Such method      http://my.oschina.net/chi ...

  8. stay Linux build Git The server

    build Git The server https://www.cnblogs.com/dee0912/p/5815267.html Git Client installation https://www.cnblogs.com/xuwenjin/p ...

  9. Database learning MySQL Basics

    Database foundation One . Database introduction database : A warehouse for storing data sql And its norms sql yes Structured Query Language( Structured query language ) Abbreviation .SQL It's a set of operation commands built specifically for the database , It's a function ...

  10. bash Programming -grep

    grep, egrep, fgrep : Output lines that match patterns grep: Support for basic regular expression metacharacters (grep -E amount to egrep) egrep: Support extended regular expression metacharacter (egrep -G amount to grep) ...