Mallinfo, print stack, malloc hook, mtrace ()

Source: Internet
Author: User

Mallinfo, print stack, malloc hook, mtrace ()

I. Instant memory status:

Void getmemstatus ()
{
Struct mallinfo info = mallinfo ();
Printf ("arena = % d/N", info. arena );
Printf ("ordblks = % d/N", info. ordblks );
Printf ("smblks = % d/N", info. smblks );
Printf ("hblks = % d/N", info. hblks );
Printf ("hblkhd = % d/N", info. hblkhd );
Printf ("usmblks = % d/N", info. usmblks );
Printf ("fsmblks = % d/N", info. fsmblks );
Printf ("uordblks = % d/N", info. uordblks );
Printf ("fordblks = % d/N", info. fordblks );
Printf ("keepcost = % d/N", info. keepcost );
}

Usage:

# Include <malloc. h>
 
Struct mallinfo [data type]
This structure type is used to return information about the dynamic memory allocator.
It contains the following members:
Int arena
This is the total size of memory allocated with sbrk by malloc, in bytes.
Int ordblks
This is the number of chunks not in use. (The Memory Allocator internally gets chunks of memory from the operating system, and then carves them up to satisfy individual malloc requests; see section 3.2.2.6 [eciency considerations for malloc], page 36 .)
Int smblks
This field is unused.
Int hblks
This is the total number of chunks allocated with MMAP.
Int hblkhd
This is the total size of memory allocated with MMAP, in bytes.
Int usmblks
This field is unused.
Int fsmblks
This field is unused.
Int uordblks
This is the total size of memory occupied by chunks handed out by malloc.
Int fordblks
This is the total size of memory occupied by free (not in use) chunks.
Int keepcost
This is the size of the top-most releasable chunk that normally borders the end of the heap (I. e., the high end of the virtual address space's data segment ).
Struct mallinfo (void) [function]
This function returns information about the current dynamic memory usage in a structure of Type struct mallinfo.

Ii. Print the stack:

# Include <execinfo. h>

# Include <stdio. h>

# Include <stdlib. h>

/* Obtain a backtrace and print it to stdout .*/

Void print_trace (void)

{

Void * array [10];

Size_t size;

Size_t I;

Size = backtrace (array, 10 );

Printf ("obtained % ZD stack frames./N", size );

For (I = 0; I <size; I ++)

Printf ("% P/N", array [I]);

}

/* A dummy function to make the backtrace more interesting .*/

Void dummy_function (void)

{

Print_trace ();

}

Int main (void)

{

Dummy_function ();

Return 0;

}

Iii. malloc Hook Function

Static void * (* old_malloc_hook) (size_t, const void *);
Static void (* old_free_hook) (void *, const void *);
Static void my_init_hook (void );
Static void * my_malloc_hook (size_t, const void *);
Static void my_free_hook (void *, const void *);

Static void my_init_hook (void)
{
Old_malloc_hook = _ malloc_hook;
Old_free_hook = _ free_hook;
_ Malloc_hook = my_malloc_hook;
_ Free_hook = my_free_hook;
}

Static void * my_malloc_hook (size_t size, const void * caller)
{
Void * result;
// Print_trace ();
_ Malloc_hook = old_malloc_hook;
Result = malloc (size );
Old_malloc_hook = _ malloc_hook;
Phone_debug_print ("/N @ % P + % P 0x % x/N", caller, result, (unsigned long INT) size );
_ Malloc_hook = my_malloc_hook;

Return result;
}

Static void my_free_hook (void * PTR, const void * caller)
{
_ Free_hook = old_free_hook;
Free (PTR );
Old_free_hook = _ free_hook;
Phone_debug_print ("/N @ % P-% P/N", caller, PTR );
_ Free_hook = my_free_hook;
}

Just need call my_init_hook () at the check point.

Iv. Check Memory Leak

 

Mtrace is used for tracking.
 
You can use muntrace to stop a trail.
 
Memory leakage check method (for Linux ):

If you want to read the original document, see "Allocation debugging" in glibc info"
Chapter 1 (execute info libc );
Glibc provides a method to check for memory leaks, provided that your program uses standard glibc functions.
Allocate memory (such as malloc, alloc ...):

1. Call void mtrace (void) (IN mcheck. h) at the beginning of the Code that requires memory leak check.
? With declarations). mtrace installs hooks for functions such as malloc to record memory allocation information.
Call void muntrace (void) at the end of the Code that requires memory leak check ).
Note: In general, do not call muntrace, but let the program end naturally, because there may be some
The memory code to be released must be run after muntrace.

2. Compile the checked code in debug mode (-G or-ggdb)

3. Set the environment variable malloc_trace to a file name, which contains memory allocation information.

4. Run the checked program until it is finished or muntrace is called.

5. Use the mtrace command to parse the memory allocation log file ($ malloc_trace)
(Mtrace Foo $ malloc_trace, where foo is the executible name)
In case of Memory leakage, mtrace will output distribution Leakage
Memory code location and allocated quantity.

Other things

1. You can add mtrace and muntrace to the signal processing function (usr1, usr2) to dynamically perform
Memory leakage check control.

2. mtrace is a Perl code. If you are interested in the conversion of symbolic addresses and code texts, you can
Read it.

3. Again, try not to use muntrace ()

 

1 # include <mcheck. h>
2
3 int main ()
4 {
5 mtrace ();
6 malloc (10 );
7 malloc (16 );
8 return 0;
9}

$ Gcc-g a. C # Remember to compile the "-G" debugging Option
$ Export malloc_trace = A. Log
$./A. Out
$ Unset malloc_trace # Remember the unset variable after execution; otherwise, other commands may overwrite the log
$ Mtrace A. Out A. Log
Memory not freed:
-----------------
Address size caller
0x09b08378 0xa at/XXX/a. C: 6
0x09b08388 0x10 at/XXX/a. C: 7

As you can see, the location of the Code that does not release the dynamic space is displayed.

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.