.\" Copyright (c) 2012 by Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH MALLINFO 3 2020\-11\-01 Linux "Linux Programmer's Manual" .SH 名前 mallinfo \- メモリー割り当て情報を取得する .SH 書式 \fB#include \fP .PP \fBstruct mallinfo mallinfo(void);\fP .SH 説明 \fBmallinfo\fP() 関数は、 \fBmalloc\fP(3) や一連の関数により実行されたメモリー割り当てに関する情報を格納した構造体のコピーを返す。 .PP Note that not all allocations are visible to \fBmallinfo\fP(); see BUGS and consider using \fBmalloc_info\fP(3) instead. .PP The returned structure is defined as follows: .PP .in +4n .EX 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) */ }; .EE .in .PP \fImallinfo\fP 構造体の各フィールドには以下の情報が格納される。 .TP 10 \fIarena\fP \fBmmap\fP(2) 以外の手段で割り当てられた総メモリー量 (例えばヒープに割り当てられたメモリー) 。 この数字には使用中のブロックやフリーリスト上のブロックも含まれる。 .TP \fIordblks\fP 通常の (つまり fastbin ではない) 未使用ブロック数。 .TP \fIsmblks\fP .\" the glibc info page wrongly says this field is unused .\" https://sourceware.org/bugzilla/show_bug.cgi?id=26746 fastbin 未使用ブロック数 (\fBmallopt\fP(3) 参照)。 .TP \fIhblks\fP \fBmmap\fP(2) を使って現在割り当てられているブロック数 (\fBmallopt\fP(3) の \fBM_MMAP_THRESHOLD\fP の議論を参照)。 .TP \fIhblkhd\fP \fBmmap\fP(2) を使って現在割り当てられているブロックのバイト数。 .TP \fIusmblks\fP .\" It seems to have been zero since at least as far back as glibc 2.15 This field is unused, and is always 0. Historically, it was the "highwater mark" for allocated space\(emthat is, the maximum amount of space that was ever allocated (in bytes); this field was maintained only in nonthreading environments. .TP \fIfsmblks\fP .\" the glibc info page wrongly says this field is unused .\" https://sourceware.org/bugzilla/show_bug.cgi?id=26746 fastbin フリーブロックの総バイト数。 .TP \fIuordblks\fP 使用中の割り当てメモリーで使われているバイト数。 .TP \fIfordblks\fP フリーブロックの総バイト数。 .TP \fIkeepcost\fP .\" .SH VERSIONS .\" Available already in glibc 2.0, possibly earlier ヒープの一番上にある解放可能な未使用の空間の大きさ。 この値は \fBmalloc_trim\fP(3) で理想的な場合に解放できる最大のバイト数である (理想的というのは、 ページ境界の制限などを無視した場合である。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lb lb lbw28 l l l. インターフェース 属性 値 T{ \fBmallinfo\fP() T} Thread safety MT\-Unsafe init const:mallopt .TE .sp 1 \fBmallinfo\fP() would access some global internal objects. If modify them with non\-atomically, may get inconsistent results. The identifier \fImallopt\fP in \fIconst:mallopt\fP mean that \fBmallopt\fP() would modify the global internal objects with atomics, that make sure \fBmallinfo\fP() is safe enough, others modify with non\-atomically maybe not. .SH 準拠 この関数は POSIX や C 標準では規定されていない。 多くの System V 由来のシステムに同様の関数が存在し、 SVID では同様の関数が規定されていた。 .SH バグ .\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=208 .\" See the 24 Aug 2011 mail by Paul Pluzhnikov: .\" "[patch] Fix mallinfo() to accumulate results for all arenas" .\" on libc-alpha@sourceware.org \fBこれらの情報はメインのメモリー割り当て領域に対するもののみである。\fP 他の領域の割り当ては対象にならない。 他の領域の情報も取得できる別の手段については \fBmalloc_stats\fP(3) や \fBmalloc_info\fP(3) を参照。 .PP \fImallinfo\fP 構造体の各フィールドは \fIint\fP 型である。 しかし、 いくつかの内部管理用の値は \fIlong\fP 型の場合もあるので、 報告される値が一周してしまい、 不正確になる可能性がある。 .SH 例 以下のプログラムは \fBmallinfo\fP() を利用して、 メモリーブロックの割り当て、解放のそれぞれ前後でメモリー割り当ての統計情報を取得する。 統計情報は標準出力に表示される。 .PP 最初の 2 つのコマンドライン引数は \fBmalloc\fP(3) で割り当てるブロック数とサイズを指定する。 .PP 残りの 3 つの引数は、どの割り当てられたブロックを \fBfree\fP(3) で解放すべきかを指定する。 これらの 3 つの引数の指定は任意で、 最初のものから順に以下の情報を指定する。 1 つ目の引数は、 ブロックを解放するループで使用するステップサイズを指定する (デフォルト値は 1 で、 1 は指定した範囲のすべてのブロックを解放することを意味する)。 2 つ目の引数は、 解放する先頭ブロックの場所番号を指定する (デフォルト値は 0 で、 0 は先頭の割り当て済みブロックを意味する)。 3 つ目の引数は、 解放する最終ブロックの場所番号よりひとつ大きい値を指定する (デフォルト値は最大ブロック番号よりもひとつ大きい値である)。 これらの 3 つの引数が省略された場合、 デフォルトではすべての割り当てられたブロックが解放される。 .PP 以下のプログラムの実行例では、 100 バイトの割り当てを 1000 回実行し、 割り当てたブロックの 2 つに 1 つを解放する。 .PP .in +4n .EX $ \fB./a.out 1000 100 2\fP ============== 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 .EE .in .SS プログラムのソース \& .EX #include #include #include static void display_mallinfo(void) { struct mallinfo mi; mi = mallinfo(); printf("Total non\-mmapped bytes (arena): %d\en", mi.arena); printf("# of free chunks (ordblks): %d\en", mi.ordblks); printf("# of free fastbin blocks (smblks): %d\en", mi.smblks); printf("# of mapped regions (hblks): %d\en", mi.hblks); printf("Bytes in mapped regions (hblkhd): %d\en", mi.hblkhd); printf("Max. total allocated space (usmblks): %d\en", mi.usmblks); printf("Free bytes held in fastbins (fsmblks): %d\en", mi.fsmblks); printf("Total allocated space (uordblks): %d\en", mi.uordblks); printf("Total free space (fordblks): %d\en", mi.fordblks); printf("Topmost releasable block (keepcost): %d\en", mi.keepcost); } int main(int argc, char *argv[]) { #define MAX_ALLOCS 2000000 char *alloc[MAX_ALLOCS]; int numBlocks, freeBegin, freeEnd, freeStep; size_t blockSize; if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) { fprintf(stderr, "%s num\-blocks block\-size [free\-step " "[start\-free [end\-free]]]\en", 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 ==============\en"); display_mallinfo(); for (int j = 0; j < numBlocks; j++) { if (numBlocks >= MAX_ALLOCS) { fprintf(stderr, "Too many allocations\en"); exit(EXIT_FAILURE); } alloc[j] = malloc(blockSize); if (alloc[j] == NULL) { perror("malloc"); exit(EXIT_FAILURE); } } printf("\en============== After allocating blocks ==============\en"); display_mallinfo(); for (int j = freeBegin; j < freeEnd; j += freeStep) free(alloc[j]); printf("\en============== After freeing blocks ==============\en"); display_mallinfo(); exit(EXIT_SUCCESS); } .EE .SH 関連項目 .ad l .nh \fBmmap\fP(2), \fBmalloc\fP(3), \fBmalloc_info\fP(3), \fBmalloc_stats\fP(3), \fBmalloc_trim\fP(3), \fBmallopt\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。