使用 malloc_trim()

Linux 专栏收录该内容
255 篇文章 1 订阅

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

测试代码如下:

#include <malloc.h>
#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


  • 1
    点赞
  • 0
    评论
  • 7
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值