【问题标题】:64-bit-capable alternative to mallinfo?mallinfo 的 64 位替代品?
【发布时间】:2017-04-14 04:11:10
【问题描述】:

在 Linux 上,我们有一个名为 mallinfo() 的(GNU C 库)函数,它为您提供了一些与内存分配相关的数字:

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)。

我猜这在某种程度上已被弃用,并且可以使用其他一些工具来获取相同的信息。那个替代设施是什么?

【问题讨论】:

    标签: linux memory-management malloc


    【解决方案1】:

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

    malloc_info() 函数旨在解决 malloc_stats(3) 和 mallinfo(3)。

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

    例如在我的系统上(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>
    

    【讨论】:

    • malloc_info 的值似乎与“int arena;”匹配价值。像“int uordblks;”这样的其他数据呢?我更感兴趣的是我的代码尝试分配了多少,而不是从系统中获取的总内存,包括所有开销。
    猜你喜欢
    • 2011-11-19
    • 2012-02-20
    • 1970-01-01
    • 2019-11-26
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多