使用 malloc_trim()

使用 malloc_trim()可把之前分配的内存还给系统。

测试代码如下:

#include 
#define NUM_CHUNKS 1000000
#define CHUNCK_SIZE 100
int main()
{
	// disable fast bins
	mallopt(M_MXFAST, 0);
	void **array = (void **) malloc(sizeof(void *) * NUM_CHUNKS);
	// allocating memory
	for (unsigned int i = 0; i < NUM_CHUNKS; i++)
	{
		array[i] = malloc(CHUNCK_SIZE);
	}
	// releasing memory ALMOST all memory
	for (unsigned int i = 0; i < NUM_CHUNKS - 1; i++)
	{
		free(array[i]);
	}
	// when enabled memory consumption reduces
	//int ret = malloc_trim(0);
	printf("ret=%d\n", ret);
	malloc_stats();
	sleep(100000);
}

没有使用 malloc_trim()时候的测试结果如下:

 ./test 
Arena 0:
system bytes     =  104079360
in use bytes     =        104
Total (incl. mmap):
system bytes     =  108081152
in use bytes     =    4001896
max mmap regions =          1
max mmap bytes   =    4001792

08521000-0e863000 rw-p 00000000 00:00 0          [heap]
Size:             101640 kB
Rss:              101564 kB
Pss:              101564 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:    101564 kB
Referenced:       101564 kB
Anonymous:        101564 kB
AnonHugePages:         0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB
SwapPss:               0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB
VmFlags: rd wr mr mw me ac
b71b7000-b7588000 rw-p 00000000 00:00 0
Size:               3908 kB
Rss:                3908 kB
Pss:                3908 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:      3908 kB
Referenced:         3908 kB
Anonymous:          3908 kB
AnonHugePages:         0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB
SwapPss:               0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB

使用 malloc_trim()后:

 ./test 
ret=1
Arena 0:
system bytes     =  104001536
in use bytes     =       1136
Total (incl. mmap):
system bytes     =  108003328
in use bytes     =    4002928
max mmap regions =          1
max mmap bytes   =    4001792

0835a000-0e689000 rw-p 00000000 00:00 0          [heap]
Size:             101564 kB
Rss:                   8 kB
Pss:                   8 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:         8 kB
Referenced:            8 kB
Anonymous:             8 kB
AnonHugePages:         0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB
SwapPss:               0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB
VmFlags: rd wr mr mw me ac
b71ba000-b758b000 rw-p 00000000 00:00 0
Size:               3908 kB
Rss:                3908 kB
Pss:                3908 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:      3908 kB
Referenced:         3908 kB
Anonymous:          3908 kB
AnonHugePages:         0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB


你可能感兴趣的:(Linux)