linux

linux中如何查看glibc内存分配情况

小亿
151
2024-04-10 10:46:37
栏目: 智能运维

在Linux中,可以使用工具如malloc_infomtracevalgrindgdb等来查看glibc内存分配情况。

例如,可以使用mallinfo函数来查看glibc内存分配情况,示例代码如下:

#include <stdio.h>
#include <malloc.h>

int main() {
    struct mallinfo mi = mallinfo();
    printf("Total non-mmapped bytes (arena): %d\n", mi.arena);
    printf("# of free chunks (ordblks): %d\n", mi.ordblks);
    printf("# of fastbin blocks (smblks): %d\n", mi.smblks);
    printf("# of mmapped regions (hblks): %d\n", mi.hblks);
    printf("Space in mmapped regions (hblkhd): %d\n", mi.hblkhd);
    printf("Maximum total allocated space (usmblks): %d\n", mi.usmblks);
    printf("Free space (fsmblks): %d\n", mi.fsmblks);
    printf("Total allocated space (uordblks): %d\n", mi.uordblks);
    printf("Total free space (fordblks): %d\n", mi.fordblks);
    printf("Topmost releasable block (keepcost): %d\n", mi.keepcost);
    
    return 0;
}

编译并运行上述代码,即可查看glibc内存分配情况。

0
看了该问题的人还看了