引用
OTP-8941 == erts ==
From this release, the previously experimental halfword
emulator is now official. It can be enabled by giving the
--enable-halfword-emulator option to the configure script.
The halfword emulator is a 64-bit application, but uses
halfwords (32-bit words) for all data in Erlang processes,
therefore using less memory and being faster than the
standard 64-bit emulator. The total size of all BEAM code and
all process data for all processes is limited to 4Gb, but ETS
tables and off-heap binaries are only limited by the amount
of available memory.
看上面的Erlang的halfword描述,貌似halfword特性很给力啊。对于Processes的寻址是32位,以为着更少的内存,更快的访问,受到4Gb的限制。可是对于tables(ets/mnesia)和off-heap binaries是64位的寻址。
我们来测试一下省了多少内存:
1、 ./configure; make ; sudo make install (64bit version)
erl +A 500 +K true
1> mnesia:start(),
1> rd(test, {k,v}),
1> mnesia:create_table(test, [{ram_copies, [node()]}, {attributes, record_info(fields, test)} PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11879 witeman 20 0 4269m 77m 2408 S 0 2.0 0:14.90 beam.smp
]),
1> lists:foreach(fun(X) -> mnesia:d PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11879 witeman 20 0 4269m 77m 2408 S 0 2.0 0:14.90 beam.smp
irty_write(#test{k = X, v = X}) end, lists:seq(1000001,2000000)),
1> mnesia:table_info(test, memory).
8144942
2> mnesia:info().
test : with 1000000 records occupying 8144942 words of mem
schema : with 2 records occupying 522 words of mem
3> memory().
[{total,116395728},
{processes,26512432},
{processes_used,26493672},
{system,89883296},
{atom,606233},
{atom_used,578661},
{binary,739408},
{code,5069980},
{ets,81482024}]
top:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9625 witeman 20 0 322m 113m 2424 S 0 2.9 0:29.57 beam.smp
2、./configure --enable-halfword-emulator; make ; sudo make install (64bit halfword version)
1> mnesia:start(),
1> rd(test, {k,v}),
1> mnesia:create_table(test, [{ram_copies, [node()]}, {attributes, record_info(fields, test)}]),
1> lists:foreach(fun(X) -> mnesia:dirty_write(#test{k = X, v = X}) end, lists:seq(1000001,2000000)),mnesia:table_info(test, memory).
11289878
2> mnesia:info().8144942
test : with 1000000 records occupying 11289878 words of mem
schema : with 2 records occupying 822 words of mem
3> memory().
[{total,73691680},
{processes,916040},
{processes_used,903440},
{system,72775640},
{atom,602993},
{atom_used,573864},
{binary,70896},
{code,4821930},
{ets,65397888},
{low,5418320}]
4> erlang:system_info(wordsize).
4
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11879 witeman 20 0 4269m 77m 2408 S 0 2.0 0:14.90 beam.smp
mnesia对比:
64bit version: 8144942 * 8 bytes = 65159536 bytes
64bit halfword version: 11289878 * 4 = 45159512 bytes
rate = 45159512 / 65159536 = 69.31%,也就是说halfword对于mnesia里的integer能减少30.69%的内存。
total memory对比:
64bit version: 116395728 bytes
64bit halfword version: 73691680 bytes
rate = 73691680 / 116395728 = 63.31%,也就是说halfword对于整个Erlang VM来说能减少36.69%的内存使用
整个VM内存使用在OS层面的对比:
64bit version: 112 m
64bit halfword version: 77 m
rate = 77 / 112 = 68.75%,也就是说halfword对于VM在OS层面上来说能减少31.25%的内存使用