在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內存分配情況。