2016-11-30 76 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>