在Linux中,可以使用工具如malloc_info
、mtrace
、valgrind
、gdb
等来查看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内存分配情况。