在Linux中,可以使用工具如malloc_info
、mtrace
、valgrind
、gdb
等来查看glibc内存分配情况。
例如,可以使用mallinfo
函数来查看glibc内存分配情况,示例代码如下:
#include
#include
struct mallinfo mi = mallinfo();
printf(“Total non-mmapped bytes (arena): %dn”, mi.arena);
printf(“# of free chunks (ordblks): %dn”, mi.ordblks);
printf(“# of fastbin blocks (smblks): %dn”, mi.smblks);
printf(“# of mmapped regions (hblks): %dn”, mi.hblks);
printf(“Space in mmapped regions (hblkhd): %dn”, mi.hblkhd);
printf(“Maximum total allocated space (usmblks): %dn”, mi.usmblks);
printf(“Free space (fsmblks): %dn”, mi.fsmblks);
printf(“Total allocated space (uordblks): %dn”, mi.uordblks);
printf(“Total free space (fordblks): %dn”, mi.fordblks);
printf(“Topmost releasable block (keepcost): %dn”, mi.keepcost);
return 0;
}
编译并运行上述代码,即可查看glibc内存分配情况。