mallinfo(3)

πŸ’– mallinfo, mallinfo2 β€” obtain memory allocation information πŸ’–

okay bestie, come SIT. we're doing a Library Functions Manual deep dive and this one is literally about peeking inside your allocator's diary. no cap. πŸ¦‹

✨ NAME ✨

mallinfo, mallinfo2 β€” obtain memory allocation information

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc)

🌸 SYNOPSIS 🌸

#include <malloc.h>

struct mallinfo mallinfo(void);
struct mallinfo2 mallinfo2(void);

πŸ‘‘ DESCRIPTION πŸ‘‘

so basically: these functions hand you back a copy of a struct stuffed with info about the memory allocations that malloc(3) and its little friend group have been doing. πŸ’…

both functions return a struct with the exact same fields. BUT β€” and this is the tea β€” the older one, mallinfo(), is deprecated because the type used for its fields is too small (go look at BUGS below, it's messy 🫠).

heads up bestie: not every single allocation is visible to these functions. see BUGS, and lowkey consider using malloc_info(3) instead. it's giving more complete.

The mallinfo2 structure returned by mallinfo2() is defined like this:

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

The mallinfo structure returned by the deprecated mallinfo() function is exactly the same, except the fields are typed as int. yeah. int. we'll get to why that's a problem. πŸ™ˆ

Here's what every single field is actually telling you:

  • arena β€” the total amount of memory allocated by means other than mmap(2) (i.e., the memory allocated on the heap). this number counts both the in-use blocks AND the blocks chillin' on the free list.
  • ordblks β€” the number of ordinary (i.e., non-fastbin) free blocks.
  • smblks β€” the number of fastbin free blocks (peep mallopt(3)).
  • hblks β€” the number of blocks currently allocated using mmap(2). (go read the discussion of M_MMAP_THRESHOLD in mallopt(3), iykyk.)
  • hblkhd β€” the number of bytes in blocks currently allocated using mmap(2).
  • usmblks β€” this field is unused and is always 0. historically it was the "highwater mark" for allocated space β€” like, the maximum amount of space that was ever allocated (in bytes). this field was only maintained in nonthreading environments. rip. πŸ’€
  • fsmblks β€” the total number of bytes in fastbin free blocks.
  • uordblks β€” the total number of bytes used by in-use allocations.
  • fordblks β€” the total number of bytes in free blocks.
  • keepcost β€” the total amount of releasable free space at the top of the heap. this is the max number of bytes that could ideally (i.e., ignoring page alignment restrictions and so on) be released by malloc_trim(3).

🧡 ATTRIBUTES 🧡

for an explanation of the terms used in this section, see attributes(7).

  • Interface: mallinfo(), mallinfo2()
  • Attribute: Thread safety
  • Value: MT-Unsafe init const:mallopt

mallinfo()/mallinfo2() would access some global internal objects. if you modify them with non-atomically, may get inconsistent results. the identifier mallopt in const:mallopt mean that mallopt() would modify the global internal objects with atomics, that make sure mallinfo()/mallinfo2() is safe enough β€” others modify with non-atomically maybe not. 🫢

πŸ“œ STANDARDS πŸ“œ

None. literally none. it's giving ✨vibes only✨.

πŸ•°οΈ HISTORY πŸ•°οΈ

  • mallinfo() β€” glibc 2.0. SVID.
  • mallinfo2() β€” glibc 2.33.

πŸ› BUGS πŸ›

Information is returned for only the main memory allocation area. allocations in other arenas are excluded. for alternatives that do include info about other arenas, see malloc_stats(3) and malloc_info(3).

The fields of the mallinfo structure returned by the older mallinfo() function are typed as int. however β€” and this is the part that gets you β€” because some internal bookkeeping values may be of type long, the reported values may wrap around zero and thus be inaccurate. so like. don't trust it fully. 😭


πŸ§ͺ EXAMPLES πŸ§ͺ

The program below employs mallinfo2() to retrieve memory allocation statistics before and after allocating and freeing some blocks of memory. the stats get displayed on standard output.

The first two command-line arguments specify the number and size of blocks to be allocated with malloc(3).

The remaining three arguments specify which of the allocated blocks should be freed with free(3). These three arguments are optional, and they specify (in order): the step size to be used in the loop that frees blocks (the default is 1, meaning free all blocks in the range); the ordinal position of the first block to be freed (default 0, meaning the first allocated block); and a number one greater than the ordinal position of the last block to be freed (default is one greater than the maximum block number). if you omit all three of these arguments, then the defaults cause all allocated blocks to be freed.

In the following example run of the program, 1000 allocations of 100 bytes are performed, and then every second allocated block is freed:

$ ./a.out 1000 100 2;
============== Before allocating blocks ==============
Total non-mmapped bytes (arena):       0
# of free chunks (ordblks):            1
# of free fastbin blocks (smblks):     0
# of mapped regions (hblks):           0
Bytes in mapped regions (hblkhd):      0
Max. total allocated space (usmblks):  0
Free bytes held in fastbins (fsmblks): 0
Total allocated space (uordblks):      0
Total free space (fordblks):           0
Topmost releasable block (keepcost):   0

============== After allocating blocks ==============
Total non-mmapped bytes (arena):       135168
# of free chunks (ordblks):            1
# of free fastbin blocks (smblks):     0
# of mapped regions (hblks):           0
Bytes in mapped regions (hblkhd):      0
Max. total allocated space (usmblks):  0
Free bytes held in fastbins (fsmblks): 0
Total allocated space (uordblks):      104000
Total free space (fordblks):           31168
Topmost releasable block (keepcost):   31168

============== After freeing blocks ==============
Total non-mmapped bytes (arena):       135168
# of free chunks (ordblks):            501
# of free fastbin blocks (smblks):     0
# of mapped regions (hblks):           0
Bytes in mapped regions (hblkhd):      0
Max. total allocated space (usmblks):  0
Free bytes held in fastbins (fsmblks): 0
Total allocated space (uordblks):      52000
Total free space (fordblks):           83168
Topmost releasable block (keepcost):   31168

πŸ’» Program source πŸ’»

#include <malloc.h>
#include <stdlib.h>
#include <string.h>

#define streq(...)  (strcmp(__VA_ARGS__) == 0)

static void
display_mallinfo2(void)
{
    struct mallinfo2 mi;

    mi = mallinfo2();

    printf("Total non-mmapped bytes (arena):       %zu\n", mi.arena);
    printf("# of free chunks (ordblks):            %zu\n", mi.ordblks);
    printf("# of free fastbin blocks (smblks):     %zu\n", mi.smblks);
    printf("# of mapped regions (hblks):           %zu\n", mi.hblks);
    printf("Bytes in mapped regions (hblkhd):      %zu\n", mi.hblkhd);
    printf("Max. total allocated space (usmblks):  %zu\n", mi.usmblks);
    printf("Free bytes held in fastbins (fsmblks): %zu\n", mi.fsmblks);
    printf("Total allocated space (uordblks):      %zu\n", mi.uordblks);
    printf("Total free space (fordblks):           %zu\n", mi.fordblks);
    printf("Topmost releasable block (keepcost):   %zu\n", mi.keepcost);
}

int
main(int argc, char *argv[])
{
#define MAX_ALLOCS 500000
    char *alloc[MAX_ALLOCS];
    size_t blockSize, numBlocks, freeBegin, freeEnd, freeStep;

    if (argc < 3 || streq(argv[1], "--help")) {
        fprintf(stderr, "%s num-blocks block-size [free-step "
                "[start-free [end-free]]]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    numBlocks = atoi(argv[1]);
    blockSize = atoi(argv[2]);
    freeStep = (argc > 3) ? atoi(argv[3]) : 1;
    freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
    freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;

    printf("============== Before allocating blocks ==============\n");
    display_mallinfo2();

    for (size_t j = 0; j < numBlocks; j++) {
        if (numBlocks >= MAX_ALLOCS) {
            fprintf(stderr, "Too many allocations\n");
            exit(EXIT_FAILURE);
        }

        alloc[j] = malloc(blockSize);
        if (alloc[j] == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
    }

    printf("\n============== After allocating blocks ==============\n");
    display_mallinfo2();

    for (size_t j = freeBegin; j < freeEnd; j += freeStep)
        free(alloc[j]);

    printf("\n============== After freeing blocks ==============\n");
    display_mallinfo2();

    exit(EXIT_SUCCESS);
}

πŸ‘€ SEE ALSO πŸ‘€

mmap(2), malloc(3), malloc_info(3), malloc_stats(3), malloc_trim(3), mallopt(3)


okay that's the whole page bestie, you ate it, periodt. πŸ’…βœ¨ this little beauty came from the Linux man-pages crew, version 6.20, dated 2026-02-22, and it's the one and only mallinfo(3). go forth and free your blocks responsibly. πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms