mallinfo, 打印堆栈, malloc钩子, mtrace()

来源:互联网 发布:网上兼职淘宝客服是真的吗 编辑:程序博客网 时间:2024/04/27 01:14

 http://blog.csdn.net/wallacexiang/article/details/4393031

 

mallinfo, 打印堆栈, malloc钩子, mtrace()



一:获得即时内存状态:

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 mallinfo (void) [Function]
    This function returns information about the current dynamic memory usage in a structure of type struct mallinfo.


二:打印堆栈:

     #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;

     }

三:malloc钩子函数

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.

四:check memory leak

 

想要跟踪的时候用mtrace。
 
停止跟踪可以使用muntrace.
 
内存泄漏检查方法(for Linux) :

如果你更想读原始文档, 请参考glibc info的"Allocation Debugging"
一章 (执行info libc);
glibc提供了一个检查内存泄漏的方法, 前提是你的程序使用glibc的标准函数
分配内存(如malloc, alloc...):

1. 在需要内存泄漏检查的代码的开始调用void mtrace(void) (在mcheck.h中
? 有声明). mtrace为malloc等函数安装hook, 用于记录内存分配信息.
在需要内存泄漏检查的代码的结束调用void muntrace(void).
注意: 一般情况下不要调用muntrace, 而让程序自然结束. 因为可能有些
释放内存代码要到muntrace之后才运行.

2. 用debug模式编译被检查代码(-g或-ggdb)

3. 设置环境变量MALLOC_TRACE为一文件名, 这一文件将存有内存分配信息.

4. 运行被检查程序, 直至结束或muntrace被调用.

5. 用mtrace命令解析内存分配Log文件($MALLOC_TRACE)
(mtrace foo $MALLOC_TRACE, where foo is the executible name)
如果有内存泄漏, mtrace会输出分配泄漏
内存的代码位置,以及分配数量.


其他东西

1. 可以将mtrace, muntrace放入信号处理函数(USR1, USR2), 以动态地进行
内存泄漏检查控制.

2. mtrace是个perl代码, 如果你对符号地址与代码文本的转换感兴趣, 可以
读一下.

3. again, 尽量不要用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 #记得编译带-g调试选项
$export MALLOC_TRACE=a.log
$./a.out
$unset MALLOC_TRACE #记得执行完后unset变量,否则可能运行其他命令可能覆盖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

可以看到,会显示未释放动态空间的代码具体位置.