问题描述
我有一个长期使用的应用程序,它经常进行内存分配-释放.任何malloc实现都会将释放的内存返回给系统吗?
I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system?
的行为是什么
- ptmalloc 1、2(默认为glibc)或3
- dlmalloc
- tcmalloc(谷歌线程malloc)
- solaris 10-11默认的malloc和mtmalloc
- FreeBSD 8默认malloc(jemalloc)
- Ho积malloc?
如果我有一个应用程序,其白天和晚上(例如)的内存消耗可能大不相同,是否可以强制任何malloc将释放的内存返回给系统?
If I have an application whose memory consumption can be very different in daytime and nighttime (e.g.), can I force any of malloc's to return freed memory to the system?
没有这种返回释放的内存将被交换掉很多次,但是这样的内存仅包含垃圾.
Without such return freed memory will be swapped out and in many times, but such memory contains only garbage.
推荐答案
以下分析仅适用于glibc(基于ptmalloc2算法).某些选项似乎对将释放的内存返回系统很有帮助:
The following analysis applies only to glibc (based on the ptmalloc2 algorithm).There are certain options that seem helpful to return the freed memory back to the system:
-
mallopt()(在
malloc.h
中定义)确实提供了使用参数选项M_TRIM_THRESHOLD
之一设置修整阈值的选项,这表示在数据段顶部允许的最小可用内存量(以字节为单位).如果该数量低于此阈值,则glibc调用brk()
将内存返还给内核.
mallopt() (defined in
malloc.h
) does provide an option to set the trim threshold value using one of the parameter optionM_TRIM_THRESHOLD
, this indicates the minimum amount of free memory (in bytes) allowed at the top of the data segment. If the amount falls below this threshold, glibc invokesbrk()
to give back memory to the kernel.
Linux中M_TRIM_THRESHOLD
的默认值设置为128K,设置较小的值可能节省空间.
The default value of M_TRIM_THRESHOLD
in Linux is set to 128K, setting a smaller value might save space.
通过在环境变量MALLOC_TRIM_THRESHOLD_
中设置修剪阈值可以实现相同的行为,而无需绝对更改源.
The same behavior could be achieved by setting trim threshold value in the environment variable MALLOC_TRIM_THRESHOLD_
, with no source changes absolutely.
但是,使用M_TRIM_THRESHOLD
运行的初步测试程序表明,即使由malloc分配的内存确实返回了系统,最初通过brk()
请求的实际内存块(竞技场)的其余部分也趋向于被保留.
However, preliminary test programs run using M_TRIM_THRESHOLD
has shown that even though the memory allocated by malloc does return to the system, the remaining portion of the actual chunk of memory(the arena) initially requested via brk()
tends to be retained.
可以通过调用malloc_trim(pad)
(在malloc.h
中定义)来修剪内存区域并将未使用的内存返还给系统.此函数调整数据段的大小,在其末尾至少保留pad
个字节,如果可以释放少于一页的字节,则失败.段大小始终是一页的倍数,在i386上为4,096字节.
It is possible to trim the memory arena and give any unused memory back to the system by calling malloc_trim(pad)
(defined in malloc.h
). This function resizes the data segment, leaving at least pad
bytes at the end of it and failing if less than one page worth of bytes can be freed. Segment size is always a multiple of one page, which is 4,096 bytes on i386.
可以使用malloc挂钩功能来实现使用malloc_trim
修改free()
的行为.这不需要对核心glibc库进行任何源代码更改.
The implementation of this modified behaviour of free()
using malloc_trim
could be done using the malloc hook functionality. This would not require any source code changes to the core glibc library.
在glibc的免费实现中使用madvise()
系统调用.
Using madvise()
system call inside the free implementation of glibc.
这篇关于malloc实现是否将释放的内存返回给系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!