2016-11-30 97 views
4

在Linux上,我们有一个名为mallinfo这个(GNU C库)功能,让你与内存分配的一些数字:支持64位的mallinfo替代品?

struct mallinfo { 
      int arena;  /* Non-mmapped space allocated (bytes) */ 
      int ordblks; /* Number of free chunks */ 
      int smblks; /* Number of free fastbin blocks */ 
      int hblks;  /* Number of mmapped regions */ 
      int hblkhd; /* Space allocated in mmapped regions (bytes) */ 
      int usmblks; /* Maximum total allocated space (bytes) */ 
      int fsmblks; /* Space in freed fastbin blocks (bytes) */ 
      int uordblks; /* Total allocated space (bytes) */ 
      int fordblks; /* Total free space (bytes) */ 
      int keepcost; /* Top-most, releasable space (bytes) */ 
     }; 

奇怪的是,这些值通常是32位整数;(!)好吧,这实际上不会这样做,特别是对于以字节数给出的值(例如fordblks)。

我想这是不赞成的,而且有些其他设施可以获得相同的信息。什么是替代设施?

回答

1

使用malloc_info()。你需要解析它的xml输出。
malloc_info man page

的malloc_info()函数被设计成解决在缺陷 malloc_stats(3)和mallinfo(3)。

malloc_info的源代码是例如 可用here。所有变量都使用size_t进行存储并相应地打印出来,它应该可以在任何位机器上运行。

E.g.在我的系统上(glibc版本2.26)malloc_info(0, stdout)打印出以下内容:

<malloc version="1"> 
<heap nr="0"> 
<sizes> 
</sizes> 
<total type="fast" count="0" size="0"/> 
<total type="rest" count="0" size="0"/> 
<system type="current" size="135168"/> 
<system type="max" size="135168"/> 
<aspace type="total" size="135168"/> 
<aspace type="mprotect" size="135168"/> 
</heap> 
<total type="fast" count="0" size="0"/> 
<total type="rest" count="0" size="0"/> 
<total type="mmap" count="0" size="0"/> 
<system type="current" size="135168"/> 
<system type="max" size="135168"/> 
<aspace type="total" size="135168"/> 
<aspace type="mprotect" size="135168"/> 
</malloc>