412 lines
15 KiB
HTML
412 lines
15 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of MALLINFO</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>MALLINFO</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
mallinfo - obtain memory allocation information
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/malloc.h">malloc.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>struct mallinfo mallinfo(void);</B>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>mallinfo</B>()
|
|
|
|
function returns a copy of a structure containing information about
|
|
memory allocations performed by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
|
|
|
|
and related functions.
|
|
<P>
|
|
|
|
Note that not all allocations are visible to
|
|
<B>mallinfo</B>();
|
|
|
|
see BUGS and consider using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_info">malloc_info</A></B>(3)
|
|
|
|
instead.
|
|
<P>
|
|
|
|
The returned structure is defined as follows:
|
|
<P>
|
|
|
|
|
|
|
|
struct mallinfo {
|
|
<BR> int arena; /* Non-mmapped space allocated (bytes) */
|
|
<BR> int ordblks; /* Number of free chunks */
|
|
<BR> int smblks; /* Number of free fastbin blocks */
|
|
<BR> int hblks; /* Number of mmapped regions */
|
|
<BR> int hblkhd; /* Space allocated in mmapped regions (bytes) */
|
|
<BR> int usmblks; /* Maximum total allocated space (bytes) */
|
|
<BR> int fsmblks; /* Space in freed fastbin blocks (bytes) */
|
|
<BR> int uordblks; /* Total allocated space (bytes) */
|
|
<BR> int fordblks; /* Total free space (bytes) */
|
|
<BR> int keepcost; /* Top-most, releasable space (bytes) */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The fields of the
|
|
<I>mallinfo</I>
|
|
|
|
structure contain the following information:
|
|
<DL COMPACT>
|
|
<DT id="1"><I>arena</I>
|
|
|
|
<DD>
|
|
The total amount of memory allocated by means other than
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
|
|
|
|
(i.e., memory allocated on the heap).
|
|
This figure includes both in-use blocks and blocks on the free list.
|
|
<DT id="2"><I>ordblks</I>
|
|
|
|
<DD>
|
|
The number of ordinary (i.e., non-fastbin) free blocks.
|
|
<DT id="3"><I>smblks</I>
|
|
|
|
<DD>
|
|
The number of fastbin free blocks (see
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3)).
|
|
|
|
<DT id="4"><I>hblks</I>
|
|
|
|
<DD>
|
|
The number of blocks currently allocated using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2).
|
|
|
|
(See the discussion of
|
|
<B>M_MMAP_THRESHOLD</B>
|
|
|
|
in
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3).)
|
|
|
|
<DT id="5"><I>hblkhd</I>
|
|
|
|
<DD>
|
|
The number of bytes in blocks currently allocated using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2).
|
|
|
|
<DT id="6"><I>usmblks</I>
|
|
|
|
<DD>
|
|
The "highwater mark" for allocated space---that is,
|
|
the maximum amount of space that was ever allocated.
|
|
This field is maintained only in nonthreading environments.
|
|
<DT id="7"><I>fsmblks</I>
|
|
|
|
<DD>
|
|
The total number of bytes in fastbin free blocks.
|
|
<DT id="8"><I>uordblks</I>
|
|
|
|
<DD>
|
|
The total number of bytes used by in-use allocations.
|
|
<DT id="9"><I>fordblks</I>
|
|
|
|
<DD>
|
|
The total number of bytes in free blocks.
|
|
<DT id="10"><I>keepcost</I>
|
|
|
|
<DD>
|
|
The total amount of releasable free space at the top
|
|
of the heap.
|
|
This is the maximum number of bytes that could ideally
|
|
(i.e., ignoring page alignment restrictions, and so on) be released by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_trim">malloc_trim</A></B>(3).
|
|
|
|
|
|
|
|
</DL>
|
|
<A NAME="lbAE"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>mallinfo</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Unsafe init const:mallopt<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<B>mallinfo</B>()
|
|
|
|
would access some global internal objects.
|
|
If modify them with non-atomically,
|
|
may get inconsistent results.
|
|
The identifier
|
|
<I>mallopt</I>
|
|
|
|
in
|
|
<I>const:mallopt</I>
|
|
|
|
mean that
|
|
<B>mallopt</B>()
|
|
|
|
would modify the global internal objects with atomics, that make sure
|
|
<B>mallinfo</B>()
|
|
|
|
is safe enough, others modify with non-atomically maybe not.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
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.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
|
|
|
|
|
|
|
|
<B>Information is returned for only the main memory allocation area.</B>
|
|
|
|
Allocations in other arenas are excluded.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_stats">malloc_stats</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_info">malloc_info</A></B>(3)
|
|
|
|
for alternatives that include information about other arenas.
|
|
<P>
|
|
|
|
The fields of the
|
|
<I>mallinfo</I>
|
|
|
|
structure are typed as
|
|
<I>int</I>.
|
|
|
|
However, because some internal bookkeeping values may be of type
|
|
<I>long</I>,
|
|
|
|
the reported values may wrap around zero and thus be inaccurate.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below employs
|
|
<B>mallinfo</B>()
|
|
|
|
to retrieve memory allocation statistics before and after
|
|
allocating and freeing some blocks of memory.
|
|
The statistics are displayed on standard output.
|
|
<P>
|
|
|
|
The first two command-line arguments specify the number and size of
|
|
blocks to be allocated with
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3).
|
|
|
|
<P>
|
|
|
|
The remaining three arguments specify which of the allocated blocks
|
|
should be freed with
|
|
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(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.
|
|
<P>
|
|
|
|
In the following example run of the program,
|
|
1000 allocations of 100 bytes are performed,
|
|
and then every second allocated block is freed:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>./a.out 1000 100 2</B>
|
|
============== 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
|
|
<P>
|
|
============== 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
|
|
<P>
|
|
============== 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
|
|
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/malloc.h">malloc.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
static void
|
|
display_mallinfo(void)
|
|
{
|
|
<BR> struct mallinfo mi;
|
|
<P>
|
|
<BR> mi = mallinfo();
|
|
<P>
|
|
<BR> printf("Total non-mmapped bytes (arena): %d\n", mi.arena);
|
|
<BR> printf("# of free chunks (ordblks): %d\n", mi.ordblks);
|
|
<BR> printf("# of free fastbin blocks (smblks): %d\n", mi.smblks);
|
|
<BR> printf("# of mapped regions (hblks): %d\n", mi.hblks);
|
|
<BR> printf("Bytes in mapped regions (hblkhd): %d\n", mi.hblkhd);
|
|
<BR> printf("Max. total allocated space (usmblks): %d\n", mi.usmblks);
|
|
<BR> printf("Free bytes held in fastbins (fsmblks): %d\n", mi.fsmblks);
|
|
<BR> printf("Total allocated space (uordblks): %d\n", mi.uordblks);
|
|
<BR> printf("Total free space (fordblks): %d\n", mi.fordblks);
|
|
<BR> printf("Topmost releasable block (keepcost): %d\n", mi.keepcost);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
#define MAX_ALLOCS 2000000
|
|
<BR> char *alloc[MAX_ALLOCS];
|
|
<BR> int numBlocks, j, freeBegin, freeEnd, freeStep;
|
|
<BR> size_t blockSize;
|
|
<P>
|
|
<BR> if (argc < 3 || strcmp(argv[1], "--help") == 0) {
|
|
<BR> fprintf(stderr, "%s num-blocks block-size [free-step "
|
|
<BR> "[start-free [end-free]]]\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> numBlocks = atoi(argv[1]);
|
|
<BR> blockSize = atoi(argv[2]);
|
|
<BR> freeStep = (argc > 3) ? atoi(argv[3]) : 1;
|
|
<BR> freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
|
|
<BR> freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
|
|
<P>
|
|
<BR> printf("============== Before allocating blocks ==============\n");
|
|
<BR> display_mallinfo();
|
|
<P>
|
|
<BR> for (j = 0; j < numBlocks; j++) {
|
|
<BR> if (numBlocks >= MAX_ALLOCS) {
|
|
<BR> fprintf(stderr, "Too many allocations\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> alloc[j] = malloc(blockSize);
|
|
<BR> if (alloc[j] == NULL) {
|
|
<BR> perror("malloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("\n============== After allocating blocks ==============\n");
|
|
<BR> display_mallinfo();
|
|
<P>
|
|
<BR> for (j = freeBegin; j < freeEnd; j += freeStep)
|
|
<BR> free(alloc[j]);
|
|
<P>
|
|
<BR> printf("\n============== After freeing blocks ==============\n");
|
|
<BR> display_mallinfo();
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_info">malloc_info</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_stats">malloc_stats</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc_trim">malloc_trim</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3)
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="11"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="12"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="13"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="14"><A HREF="#lbAE">ATTRIBUTES</A><DD>
|
|
<DT id="15"><A HREF="#lbAF">CONFORMING TO</A><DD>
|
|
<DT id="16"><A HREF="#lbAG">BUGS</A><DD>
|
|
<DT id="17"><A HREF="#lbAH">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="18"><A HREF="#lbAI">Program source</A><DD>
|
|
</DL>
|
|
<DT id="19"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="20"><A HREF="#lbAK">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:47 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|