man-pages/man3/mallinfo.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
mallinfo - obtain memory allocation information
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;</B>
<P>
<B>struct mallinfo mallinfo(void);</B>
<A NAME="lbAD">&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;arena;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Non-mmapped&nbsp;space&nbsp;allocated&nbsp;(bytes)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;ordblks;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;free&nbsp;chunks&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;smblks;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;free&nbsp;fastbin&nbsp;blocks&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;hblks;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;mmapped&nbsp;regions&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;hblkhd;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Space&nbsp;allocated&nbsp;in&nbsp;mmapped&nbsp;regions&nbsp;(bytes)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;usmblks;&nbsp;&nbsp;&nbsp;/*&nbsp;Maximum&nbsp;total&nbsp;allocated&nbsp;space&nbsp;(bytes)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;fsmblks;&nbsp;&nbsp;&nbsp;/*&nbsp;Space&nbsp;in&nbsp;freed&nbsp;fastbin&nbsp;blocks&nbsp;(bytes)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;uordblks;&nbsp;&nbsp;/*&nbsp;Total&nbsp;allocated&nbsp;space&nbsp;(bytes)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;fordblks;&nbsp;&nbsp;/*&nbsp;Total&nbsp;free&nbsp;space&nbsp;(bytes)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;keepcost;&nbsp;&nbsp;/*&nbsp;Top-most,&nbsp;releasable&nbsp;space&nbsp;(bytes)&nbsp;*/
};
<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 &quot;highwater mark&quot; 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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
<P>
static void
display_mallinfo(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;mallinfo&nbsp;mi;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;mi&nbsp;=&nbsp;mallinfo();
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Total&nbsp;non-mmapped&nbsp;bytes&nbsp;(arena):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.arena);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;#&nbsp;of&nbsp;free&nbsp;chunks&nbsp;(ordblks):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.ordblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;#&nbsp;of&nbsp;free&nbsp;fastbin&nbsp;blocks&nbsp;(smblks):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.smblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;#&nbsp;of&nbsp;mapped&nbsp;regions&nbsp;(hblks):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.hblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Bytes&nbsp;in&nbsp;mapped&nbsp;regions&nbsp;(hblkhd):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.hblkhd);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Max.&nbsp;total&nbsp;allocated&nbsp;space&nbsp;(usmblks):&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.usmblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Free&nbsp;bytes&nbsp;held&nbsp;in&nbsp;fastbins&nbsp;(fsmblks):&nbsp;%d\n&quot;,&nbsp;mi.fsmblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Total&nbsp;allocated&nbsp;space&nbsp;(uordblks):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.uordblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Total&nbsp;free&nbsp;space&nbsp;(fordblks):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.fordblks);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Topmost&nbsp;releasable&nbsp;block&nbsp;(keepcost):&nbsp;&nbsp;&nbsp;%d\n&quot;,&nbsp;mi.keepcost);
}
<P>
int
main(int argc, char *argv[])
{
#define MAX_ALLOCS 2000000
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*alloc[MAX_ALLOCS];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;numBlocks,&nbsp;j,&nbsp;freeBegin,&nbsp;freeEnd,&nbsp;freeStep;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;blockSize;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;3&nbsp;||&nbsp;strcmp(argv[1],&nbsp;&quot;--help&quot;)&nbsp;==&nbsp;0)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;%s&nbsp;num-blocks&nbsp;block-size&nbsp;[free-step&nbsp;&quot;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;[start-free&nbsp;[end-free]]]\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;numBlocks&nbsp;=&nbsp;atoi(argv[1]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;blockSize&nbsp;=&nbsp;atoi(argv[2]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;freeStep&nbsp;=&nbsp;(argc&nbsp;&gt;&nbsp;3)&nbsp;?&nbsp;atoi(argv[3])&nbsp;:&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;freeBegin&nbsp;=&nbsp;(argc&nbsp;&gt;&nbsp;4)&nbsp;?&nbsp;atoi(argv[4])&nbsp;:&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;freeEnd&nbsp;=&nbsp;(argc&nbsp;&gt;&nbsp;5)&nbsp;?&nbsp;atoi(argv[5])&nbsp;:&nbsp;numBlocks;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;==============&nbsp;Before&nbsp;allocating&nbsp;blocks&nbsp;==============\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;display_mallinfo();
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;numBlocks;&nbsp;j++)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(numBlocks&nbsp;&gt;=&nbsp;MAX_ALLOCS)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Too&nbsp;many&nbsp;allocations\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alloc[j]&nbsp;=&nbsp;malloc(blockSize);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(alloc[j]&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;malloc&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n==============&nbsp;After&nbsp;allocating&nbsp;blocks&nbsp;==============\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;display_mallinfo();
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;freeBegin;&nbsp;j&nbsp;&lt;&nbsp;freeEnd;&nbsp;j&nbsp;+=&nbsp;freeStep)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(alloc[j]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n==============&nbsp;After&nbsp;freeing&nbsp;blocks&nbsp;==============\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;display_mallinfo();
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAJ">&nbsp;</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">&nbsp;</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">&nbsp;</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>