On Linux with glibc
, you can use malloc_info()
to get heap usage statisics:
SYNOPSIS
#include <malloc.h>
int malloc_info(int options, FILE *stream);
DESCRIPTION
The malloc_info()
function exports an XML string that describes
the current state of the memory-allocation implementation in the
caller. The string is printed on the file stream stream. The
exported string includes information about all arenas (see
malloc(3)).
As currently implemented, options must be zero.
That produces an XML document that you have to parse. But you might be able to use mallinfo()
to get heap usage statistics (but see the BUGS section):
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 {
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; /* See below */
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) */
};
However,
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.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…