mallinfo (3) - Linux Manuals
mallinfo: obtain memory allocation information
NAME
mallinfo - obtain memory allocation information
SYNOPSIS
#include <malloc.h>struct mallinfo mallinfo(void);
DESCRIPTION
The mallinfo() function returns a copy of a structure containing information about memory allocations performed by malloc(3) and related functions.Note that not all allocations are visible to mallinfo(); see BUGS and consider using malloc_info(3) instead.
The returned structure is defined as follows:
struct mallinfo {
The fields of the
mallinfo
structure contain the following information:
mallinfo()
would access some global internal objects.
If 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()
is safe enough, others modify with non-atomically maybe not.
The fields of the
mallinfo
structure are typed as
int.
However, because some internal bookkeeping values may be of type
long,
the reported values may wrap around zero and thus be inaccurate.
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 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 these three arguments are omitted,
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
static void
display_mallinfo(void)
{
int
main(int argc, char *argv[])
{
#define MAX_ALLOCS 2000000
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
Interface Attribute Value
mallinfo()
Thread safety MT-Unsafe init const:mallopt CONFORMING TO
This function is not specified by POSIX or the C standards.
A similar function exists on many System V derivatives,
and was specified in the SVID.
BUGS
Information is returned for only the main memory allocation area.
Allocations in other arenas are excluded.
See
malloc_stats(3)
and
malloc_info(3)
for alternatives that include information about other arenas.
EXAMPLES
The program below employs
mallinfo()
to retrieve memory allocation statistics before and after
allocating and freeing some blocks of memory.
The statistics are displayed on standard output.
Program source
#include <malloc.h>
#include <stdlib.h>
#include <string.h>