场景:
游服 erl虚拟机内存占用率过高,但是实际上的内存利用率非常低。原因是内存分配采用的是最快分配策略,会导致 1kb大小的数据占用 100kb大小的内存块,导致利用率低。
原因:
我们项目中频繁使用二进制数据,Port口请求、DB存储都有使用二进制数据,当二进制数据的长度超过64 bytes 时就会在堆内存开辟一块空间。数据库read之后,会把查询的值缓存一天,这个值占用的内存空间就不会被释放掉。
而内存利用率低的原因个人认为是因为登录数据、战报数据开辟的空间比较大,而DB读取的数据比较小,小的数据占用了大的空间,就导致了利用率较低。
解决办法:
修改内存分配策略,在启动脚本上加上:+MBas aobf +MBlmbcs 512
Erlang一共有7种分配策略
You then need to know the different memory allocation strategies of the Erlang virtual machine:
Best fit (bf)
Address order best fit (aobf)
Address order first fit (aoff)
Address order first fit carrier best fit (aoffcbf)
Address order first fit carrier address order best fit (aoffcaobf)
Good fit (gf)
A fit (af)
For best fit (bf), the VM builds a balanced binary tree of all the free blocks’ sizes, and will try to find the smallest one that will accommodate the piece of data and allocate it there. In the drawing above, having a piece of data that requires three blocks would likely end in area 3.
对于最佳拟合策略(bf),VM将构建一个包含所有空闲块大小的平衡二叉树,并尝试找到容纳数据块的最小二叉树并将其分配到那里。在上图中,拥有一段需要三个块的数据可能会以区域3结束。
Address order best fit (aobf) will work similarly, but the tree instead is based on the addresses of the blocks. So the VM w