mallinfo(3) - wariua/manpages-ko GitHub Wiki
mallinfo - ๋ฉ๋ชจ๋ฆฌ ํ ๋น ์ ๋ณด ์ป๊ธฐ
#include <malloc.h>
struct mallinfo mallinfo(void);
mallinfo()
ํจ์๋ malloc(3) ๋ฐ ๊ด๋ จ ํจ์๋ค์ด ์ํํ๋ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ๋ํ ์ ๋ณด๋ฅผ ๋ด์ ๊ตฌ์กฐ์ฒด ์ฌ๋ณธ์ ๋ฐํํ๋ค.
์ฐธ๊ณ ๋ก mallinfo()
์ ๋ชจ๋ ํ ๋น์ด ๋ณด์ด๋ ๊ฑด ์๋๋ค. BUGS๋ฅผ ์ฐธ๊ณ ํ๊ณ malloc_info(3) ์ฌ์ฉ์ ๊ฒํ ํ ์ ์๋ค.
๋ฐํ๋๋ ๊ตฌ์กฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋์ด ์๋ค.
struct mallinfo {
int arena; /* ๋น mmap ํ ๋น ๊ณต๊ฐ (๋ฐ์ดํธ) */
int ordblks; /* ์ ํด ์ฒญํฌ ์ */
int smblks; /* ์ ํด ํจ์คํธ๋น ๋ธ๋ก ์ */
int hblks; /* mmap ์์ญ ์ */
int hblkhd; /* mmap ์์ญ๋ค์ ํ ๋น ๊ณต๊ฐ (๋ฐ์ดํธ) */
int usmblks; /* ์ด ํ ๋น ๊ณต๊ฐ ์ต๋์น (๋ฐ์ดํธ) */
int fsmblks; /* ์ ํด ํจ์คํธ๋น ๋ธ๋ก๋ค์ ๊ณต๊ฐ (๋ฐ์ดํธ) */
int uordblks; /* ์ด ํ ๋น ๊ณต๊ฐ (๋ฐ์ดํธ) */
int fordblks; /* ์ด ์ ํด ๊ณต๊ฐ (๋ฐ์ดํธ) */
int keepcost; /* ์ต์๋จ์ ํด์ ๊ฐ๋ฅ ๊ณต๊ฐ (๋ฐ์ดํธ) */
};
mallinfo
๊ตฌ์กฐ์ฒด์ ํ๋๋ค์ ๋ค์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ค.
arena
- mmap(2) ์ธ์ ๋ฐฉ๋ฒ์ผ๋ก ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์ (์ฆ ํ์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์) ์ด๋. ์ด ์์น์๋ ์ฌ์ฉ ์ค์ธ ๋ธ๋ก๊ณผ ์ ํด ๋ชฉ๋ก์ ๋ธ๋ก์ด ๋ชจ๋ ํฌํจ๋๋ค.
ordblks
- ํ๋ฒํ (์ฆ ํจ์คํธ๋น ์๋) ์ ํด ๋ธ๋ก ๊ฐ์.
smblks
- ํจ์คํธ๋น ์ ํด ๋ธ๋ก ๊ฐ์. (mallopt(3) ์ฐธ๊ณ .)
hblks
- ํ์ฌ mmap(2)์ผ๋ก ํ ๋น๋ ๋ธ๋ก ๊ฐ์. (mallopt(3)์
M_MMAP_THRESHOLD
๋ ผ์ ์ฐธ๊ณ .) hblkhd
- ํ์ฌ mmap(2)์ผ๋ก ํ ๋น๋ ๋ธ๋ก๋ค์ ๋ฐ์ดํธ ์.
usmblks
- ํ ๋น ๊ณต๊ฐ์ "์ต๊ณ ์์ ์ "์ด๋ค. ์ฆ ์ง๊ธ๊น์ง ํ ๋น ๊ณต๊ฐ์ ์ต๋ ์์ด๋ค. ์ค๋ ๋๋ฅผ ์ฐ์ง ์๋ ํ๊ฒฝ์์๋ง ์ด ํ๋๋ฅผ ์ ์งํ๋ค.
fsmblks
- ํจ์คํธ๋น ์ ํด ๋ธ๋ก๋ค์ ์ด ๋ฐ์ดํธ ์.
uordblks
- ์ฌ์ฉ ์ค์ธ ํ ๋น๋ค์ ์ฐ๋ ์ด ๋ฐ์ดํธ ์.
fordblks
- ์ ํด ๋ธ๋ก๋ค์ ์ด ๋ฐ์ดํธ ์.
keepcost
- ํ ์๋จ์ ํด์ ๊ฐ๋ฅํ ์ ํด ๊ณต๊ฐ์ ์ด๋. ๋ค๋ฅธ ์ ์ฝ์ด ์๋ค๋ฉด (์ฆ ํ์ด์ง ์ ๋ ฌ ์ ์ฝ ๋ฑ์ ๋ฌด์ํ ๋) malloc_trim(3)์ผ๋ก ํด์ ๊ฐ๋ฅํ ์ต๋ ๋ฐ์ดํธ ์์ด๋ค.
์ด ์ ์์ ์ฌ์ฉํ๋ ์ฉ์ด๋ค์ ๋ํ ์ค๋ช ์ attributes(7)๋ฅผ ๋ณด๋ผ.
์ธํฐํ์ด์ค | ์์ฑ | ๊ฐ |
---|---|---|
mallinfo() |
์ค๋ ๋ ์์ ์ฑ | MT-Unsafe init const:mallopt |
mallinfo()
๋ ๋ช๋ช ์ ์ญ ๋ด๋ถ ๊ฐ์ฒด๋ค์ ์ ๊ทผํ๊ฒ ๋๋ค. ๋น์์์ ๋ณ๊ฒฝ ์ ๋ชจ์์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์๋ ์๋ค. const:mallopt
์ ์๋ณ์ mallopt
๊ฐ ๋ปํ๋ ๊ฒ์ mallopt()
๋ ์ ์ญ ๋ด๋ถ ๊ฐ์ฒด๋ฅผ ์์์ ์ผ๋ก ๋ณ๊ฒฝํ๋ฏ๋ก mallinfo()
๊ฐ ์ถฉ๋ถํ ์์ ํ ๊ฒ์ด๊ณ , ๋น์์์ ์ผ๋ก ๋ณ๊ฒฝํ๋ ๋ค๋ฅธ ํจ์๋ค์ ๊ทธ๋ ์ง ์์ ์ ์๋ค๋ ๊ฒ์ด๋ค.
์ด ํจ์๋ POSIX๋ C ํ์ค์ ๋ช ์ธ๋์ด ์์ง ์๋ค. ์ฌ๋ฌ ์์คํ V ํ์ ์์คํ ์ ๋น์ทํ ํจ์๊ฐ ์กด์ฌํ๋ฉฐ SVID์ ๋ช ์ธ๋์๋ค.
์ฃผ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ์์ญ์ ๋ํ ์ ๋ณด๋ง ๋ฐํํ๋ค. ๋ค๋ฅธ ์๋ ๋์์์ ํ ๋น์ ๋น ์ ธ ์๋ค. ๋ค๋ฅธ ์๋ ๋์ ๋ํ ์ ๋ณด๋ฅผ ํฌํจํ๋ ๋์์ผ๋ก๋ malloc_stats(3)์ malloc_info(3)๋ฅผ ๋ณด๋ผ.
mallinfo
๊ตฌ์กฐ์ฒด์ ํ๋๋ค์ด int
ํ์
์ผ๋ก ๋์ด ์๋ค. ํ์ง๋ง ๋ด๋ถ์ ๋ช๋ช ์ํ ๊ด๋ฆฌ ๊ฐ๋ค์ด long
ํ์
์ผ ์๋ ์๊ธฐ ๋๋ฌธ์ ์ป์ด ์จ ๊ฐ์ด ์๋ ค์ ๋ถ์ ํํ ์๋ ์๋ค.
์๋ ํ๋ก๊ทธ๋จ์์๋ mallinfo()
๋ฅผ ์ด์ฉํด ํ ๋น ์ ๊ณผ ํ, ๊ทธ๋ฆฌ๊ณ ์ผ๋ถ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก์ ํด์ ํ ํ์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ํต๊ณ๋ฅผ ์ป์ด ์จ๋ค. ๊ทธ ํต๊ณ๋ฅผ ํ์ค ์ถ๋ ฅ์ผ๋ก ํ์ํ๋ค.
์ฒ์ ๋ ๊ฐ์ ๋ช ๋ นํ ์ธ์๋ malloc(3)์ผ๋ก ํ ๋นํ ๋ธ๋ก์ ์์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๋ค.
๋๋จธ์ง ์ธ ์ธ์๋ ํ ๋น ๋ธ๋ก๋ค ์ค ์ด๋ ๊ฒ์ free(3)๋ก ํด์ ํ ์ง ์ง์ ํ๋ค. ์ด ์ธ ์ธ์๋ ์ ํ์ ์ด๋ฉฐ, (์ฐจ๋ก๋๋ก) ๋ธ๋ก ํด์ ๋ฃจํ ๋ด์์ ์ธ ๋จ๊ณ ํฌ๊ธฐ (๊ธฐ๋ณธ์ 1, ์ฆ ๋ฒ์ ๋ด ๋ชจ๋ ๋ธ๋ก์ ํด์ ), ํด์ ํ ์ฒซ ๋ธ๋ก์ ์๋ฒ (๊ธฐ๋ณธ์ 0, ์ฆ ์ฒซ ๋ฒ์งธ ํ ๋น ๋ธ๋ก), ํด์ ํ ๋ง์ง๋ง ๋ธ๋ก์ ์๋ฒ์ 1์ ๋ํ ๊ฒ (๊ธฐ๋ณธ์ ์ต๋ ๋ธ๋ก ๋ฒํธ์ 1์ ๋ํ ๊ฒ)์ด๋ค. ์ด ์ธ ์ธ์๋ฅผ ์๋ตํ ๊ฒฝ์ฐ์ ๊ธฐ๋ณธ๊ฐ์ ๋ชจ๋ ํ ๋น ๋ธ๋ก๋ค์ ํด์ ํ๊ฒ ํ๋ค.
๋ค์ ํ๋ก๊ทธ๋จ ์คํ ์์์๋ 100๋ฐ์ดํธ ํ ๋น์ 1000๋ฒ ์ํํ๊ณ ์ ํ ๋น ๋ธ๋ก ๋ ๊ฐ๋ง๋ค ํ๋์ฉ ํด์ ํ๋ค.
$ ./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, j, 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 (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 (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)
2019-03-06