版本

malloc_info()已在版本2.10中添加到glibc。

另外参见

mallinfo(3),malloc(3),malloc_stats(3),mallopt(3),open_memstream(3)

遵循规范

此函数是GNU扩展。

错误说明

EINVAL
选项非零。

出版信息

这个页面是Linux手册页项目5.08版的一部分。有关项目的说明、有关报告错误的信息以及此页面的最新版本,请访问https://www.kernel.org/doc/man-pages/

备注

内存分配信息以XML字符串(而不是C结构)的形式提供,因为该信息可能会随时间变化(根据底层实现的变化)。输出的XML字符串包括版本字段。

open_memstream(3)函数可用于将malloc_info()的输出直接发送到内存中的缓冲区中,而不是发送到文件中。

malloc_info()函数旨在解决malloc_stats(3)和mallinfo(3)中的缺陷。

名称

malloc_info-将malloc状态导出到流

语法

#include <malloc.h>

int malloc_info(int options, FILE *stream);
MALLOC_INFO - Linux手册页

Linux程序员手册 第3部分
更新日期: 2020-06-09

示例

下面的程序最多包含四个命令行参数,其中前三个是必需的。第一个参数指定程序应创建的线程数。所有线程(包括主线程)均分配第二个参数指定的内存块数。第三个参数控制要分配的块的大小。主线程创建该大小的块,程序创建的第二个线程分配该大小的两倍的块,第三个线程分配该大小的三倍的块,依此类推。

该程序两次调用malloc_info()以显示内存分配状态。第一次调用发生在创建任何线程或分配内存之前。在所有线程分配了内存之后执行第二次调用。

在下面的示例中,命令行参数指定了一个附加线程的创建,并且主线程和附加线程都分配了10000个内存块。分配了内存块之后,malloc_info()显示两个分配区域的状态。

$ getconf GNU_LIBC_VERSION
glibc 2.13
$ ./a.out 1 10000 100
============ Before allocating blocks ============
<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"/>
<system type="current" size="135168"/>
<system type="max" size="135168"/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</malloc>

============ After allocating blocks ============
<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="1081344"/>
<system type="max" size="1081344"/>
<aspace type="total" size="1081344"/>
<aspace type="mprotect" size="1081344"/>
</heap>
<heap nr="1">
<sizes>
</sizes>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="1032192"/>
<system type="max" size="1032192"/>
<aspace type="total" size="1032192"/>
<aspace type="mprotect" size="1032192"/>
</heap>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="2113536"/>
<system type="max" size="2113536"/>
<aspace type="total" size="2113536"/>
<aspace type="mprotect" size="2113536"/>
</malloc>

Program source

#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
#include <errno.h>

static size_t blockSize;
static int numThreads, numBlocks;

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                        } while (0)

static void *
thread_func(void *arg)
{
    int j;
    int tn = (int) arg;

    /* The multiplier aq(2 + tn)aq ensures that each thread (including
       the main thread) allocates a different amount of memory */

    for (j = 0; j < numBlocks; j++)
        if (malloc(blockSize * (2 + tn)) == NULL)
            errExit("malloc-thread");

    sleep(100);         /* Sleep until main thread terminates */
    return NULL;
}

int
main(int argc, char *argv[])
{
    int j, tn, sleepTime;
    pthread_t *thr;

    if (argc < 4) {
        fprintf(stderr,
                "%s num-threads num-blocks block-size [sleep-time]\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    numThreads = atoi(argv[1]);
    numBlocks = atoi(argv[2]);
    blockSize = atoi(argv[3]);
    sleepTime = (argc > 4) ? atoi(argv[4]) : 0;

    thr = calloc(numThreads, sizeof(pthread_t));
    if (thr == NULL)
        errExit("calloc");

    printf("============ Before allocating blocks ============\n");
    malloc_info(0, stdout);

    /* Create threads that allocate different amounts of memory */

    for (tn = 0; tn < numThreads; tn++) {
        errno = pthread_create(&thr[tn], NULL, thread_func,
                               (void *) tn);
        if (errno != 0)
            errExit("pthread_create");

        /* If we add a sleep interval after the start-up of each
           thread, the threads likely wonaqt contend for malloc
           mutexes, and therefore additional arenas wonaqt be
           allocated (see malloc(3)). */

        if (sleepTime > 0)
            sleep(sleepTime);
    }

    /* The main thread also allocates some memory */

    for (j = 0; j < numBlocks; j++)
        if (malloc(blockSize) == NULL)
            errExit("malloc");

    sleep(2);           /* Give all threads a chance to
                           complete allocations */

    printf("\n============ After allocating blocks ============\n");
    malloc_info(0, stdout);

    exit(EXIT_SUCCESS);
}

属性

有关本节中使用的术语的说明,请参见attribute(7)。

InterfaceAttributeValue
malloc_info()Thread safetyMT-Safe

说明

malloc_info()函数导出XML字符串,该字符串描述调用方中内存分配实现的当前状态。该字符串将打印在文件流流上。导出的字符串包含有关所有竞技场的信息(请参阅malloc(3))。

按照当前的实现,选项必须为零。

返回值

成功时,malloc_info()返回0;否则,返回0。如果出错,则返回-1,并设置errno以指示原因。

日期:2019-08-20 18:00:51 来源:oir作者:oir