Erlang从R13B03开始引入了vheap的概念,具体参见这篇文章:R13B03 binary vheap有助减少binary内存压力
官方的release note里面简单的解释了下:
OTP-8202 A new garbage collecting strategy for binaries which is more
aggressive than the previous implementation. Binaries now has
a virtual binary heap tied to each process. When binaries are
created or received to a process it will check if the heap
limit has been reached and if a reclaim should be done. This
imitates the behavior of ordinary Erlang terms. The virtual
heaps are grown and shrunk like ordinary heaps. This will
lessen the memory footprint of binaries in a system.
但除此之外,无法找到更细的文档。最近在做的服务器程序里面用到了大量的binary, 需要对binary的行为做详细的分析,所以就顺便把vheap好好整理下。
我们首先看下如何控制vheap.
首先看下全局的设置, 参见这里
+hmbs Size
Sets the default binary virtual heap size of processes to the size Size.
如果不设定的话,这个值默认是:
./erl_vm.h:62:#define VH_DEFAULT_SIZE 32768 /* default virtual (bin) heap min size (words) */
我们来验证下我们的设置:
Erlang R16B03 (erts-5.10.4) [source-73d1b4a] [64-bit] [smp:16:16] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1> erlang:system_info(min_bin_vheap_size).
{min_bin_vheap_size,46422}
oops, 怎么对不起来呢?
看下代码,原来在erl_init里面会对这个值再进行调整:
Read more…
摘抄自Erlang release note:
The vheap size series will now use the golden ratio instead of doubling and fibonacci sequences.
决定binary heap的大小现在是黄金分割率算法,很有意思,给大家参考下:
/* grow
*
* vheap_sz ======================
*
* vheap 75% + grow
* ———————-
*
* vheap 25 – 75% same
* ———————-
*
* vheap ~ – 25% shrink
*
* ———————-
*/
代码如下:
//erl_gc.c:2155
static Uint64
do_next_vheap_size(Uint64 vheap, Uint64 vheap_sz) {
if ((Uint64) vheap/3 > (Uint64) (vheap_sz/4)) {
Uint64 new_vheap_sz = vheap_sz;
while((Uint64) vheap/3 > (Uint64) (vheap_sz/4)) {
/* the golden ratio = 1.618 */
new_vheap_sz = (Uint64) vheap_sz * 1.618;
if (new_vheap_sz < vheap_sz ) {
return vheap_sz;
}
vheap_sz = new_vheap_sz;
}
return vheap_sz;
}
if (vheap < (Uint64) (vheap_sz/4)) {
return (vheap_sz >> 1);
}
return vheap_sz;
}
祝大家玩得开心!
R13B03 binary vheap有助减少binary内存压力.
参看:http://www.erlang.org/download/otp_src_R13B03.readme
OTP-8202 A new garbage collecting strategy for binaries which is more
aggressive than the previous implementation. Binaries now has
a virtual binary heap tied to each process. When binaries are
created or received to a process it will check if the heap
limit has been reached and if a reclaim should be done. This
imitates the behavior of ordinary Erlang terms. The virtual
heaps are grown and shrunk like ordinary heaps. This will
lessen the memory footprint of binaries in a system.
原来的binary和其他类型的eterm是混在一起的,当进程的heap用完的时候,才进行GC,腾出空间。现在是把binary单独抽取出来,单独计数, 当binary用量过大的时候,马上就GC。
这个策略对于我们的服务器程序很有帮助,因为我们的服务器程序基本上包都是binary, 很容易出现binary无法及时回收,导致内存耗光的情况。
具体的改进效果待进一步的观察。
Recent Comments