MALLINFO(3) | Linux Programmer's Manual | MALLINFO(3) |
mallinfo - メモリー割り当て情報を取得する
#include <malloc.h>
struct mallinfo mallinfo(void);
mallinfo() 関数は、 malloc(3) や一連の関数により実行されたメモリー割り当てに関する情報を格納した構造体のコピーを返す。
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) */ };
mallinfo 構造体の各フィールドには以下の情報が格納される。
この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース | 属性 | 値 |
mallinfo() | Thread safety | MT-Unsafe init const:mallopt |
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.
この関数は POSIX や C 標準では規定されていない。 多くの System V 由来のシステムに同様の関数が存在し、 SVID では同様の関数が規定されていた。
これらの情報はメインのメモリー割り当て領域に対するもののみである。 他の領域の割り当ては対象にならない。 他の領域の情報も取得できる別の手段については malloc_stats(3) や malloc_info(3) を参照。
mallinfo 構造体の各フィールドは int 型である。 しかし、 いくつかの内部管理用の値は long 型の場合もあるので、 報告される値が一周してしまい、 不正確になる可能性がある。
以下のプログラムは mallinfo() を利用して、 メモリーブロックの割り当て、解放のそれぞれ前後でメモリー割り当ての統計情報を取得する。 統計情報は標準出力に表示される。
最初の 2 つのコマンドライン引数は malloc(3) で割り当てるブロック数とサイズを指定する。
残りの 3 つの引数は、どの割り当てられたブロックを free(3) で解放すべきかを指定する。 これらの 3 つの引数の指定は任意で、 最初のものから順に以下の情報を指定する。 1 つ目の引数は、 ブロックを解放するループで使用するステップサイズを指定する (デフォルト値は 1 で、 1 は指定した範囲のすべてのブロックを解放することを意味する)。 2 つ目の引数は、 解放する先頭ブロックの場所番号を指定する (デフォルト値は 0 で、 0 は先頭の割り当て済みブロックを意味する)。 3 つ目の引数は、 解放する最終ブロックの場所番号よりひとつ大きい値を指定する (デフォルト値は最大ブロック番号よりもひとつ大きい値である)。 これらの 3 つの引数が省略された場合、 デフォルトではすべての割り当てられたブロックが解放される。
以下のプログラムの実行例では、 100 バイトの割り当てを 1000 回実行し、 割り当てたブロックの 2 つに 1 つを解放する。
$ ./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
#include <malloc.h> #include <stdlib.h> #include <string.h> static void display_mallinfo(void) { struct mallinfo mi; mi = mallinfo(); printf("Total non-mmapped bytes (arena): %d\n", mi.arena); printf("# of free chunks (ordblks): %d\n", mi.ordblks); printf("# of free fastbin blocks (smblks): %d\n", mi.smblks); printf("# of mapped regions (hblks): %d\n", mi.hblks); printf("Bytes in mapped regions (hblkhd): %d\n", mi.hblkhd); printf("Max. total allocated space (usmblks): %d\n", mi.usmblks); printf("Free bytes held in fastbins (fsmblks): %d\n", mi.fsmblks); printf("Total allocated space (uordblks): %d\n", mi.uordblks); printf("Total free space (fordblks): %d\n", mi.fordblks); printf("Topmost releasable block (keepcost): %d\n", 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]]]\n", 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 ==============\n"); display_mallinfo(); for (int j = 0; j < numBlocks; j++) { if (numBlocks >= MAX_ALLOCS) { fprintf(stderr, "Too many allocations\n"); exit(EXIT_FAILURE); } alloc[j] = malloc(blockSize); if (alloc[j] == NULL) { perror("malloc"); exit(EXIT_FAILURE); } } printf("\n============== After allocating blocks ==============\n"); display_mallinfo(); for (int j = freeBegin; j < freeEnd; j += freeStep) free(alloc[j]); printf("\n============== After freeing blocks ==============\n"); display_mallinfo(); exit(EXIT_SUCCESS); }
mmap(2), malloc(3), malloc_info(3), malloc_stats(3), malloc_trim(3), mallopt(3)
この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。
2020-11-01 | Linux |