当我在ets中插入一长串列表时,我遇到了一个奇怪的问题。内存消耗过高,如果我插入二进制文件,看起来不错,但是当我插入具有10000+个字符的列表时,如果ETS表大小仅为31MB,则它消耗的系统内存超过250 MB。有人知道发生了什么吗?
谢谢~~
埃里克
----- shell命令-----
Erlang/OTP 17 [erts-6.1] [source-d2a4c20] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V6.1 (abort with ^G)
1> c(test).
{ok,test}
2> test:new().
true
3> test:loop(test:x2()).
----Proc 0.2196502685546875 MB: ETS(1401): 31.17036533355713 MB
<0.41.0>
-----源代码,请在最后一行更改二进制数据-----
-module(test).
-export([new/0, loop/1, loop/2, x1/0, x2/0]).
get_current_process_memory() -> element(2, erlang:process_info(self(), memory))/(1024*1024).
new() ->
try
ets:new(temp, [ordered_set, named_table, public])
catch
_ -> ok
end.
%size(Item) -> erts_debug:flat_size(Item)/(1024*1024).
loop(X) ->
spawn(fun() ->
loop(X, 1400),
io:format("----Proc ~p MB: ETS(~p): ~p MB~n", [get_current_process_memory(), ets:info(temp, size), ets:info(temp, memory)/(1024*1024)])
end).
loop(_, 0) -> ok;
loop(X, Count) ->
ets:insert(temp, {Count,X}),
loop(X, Count - 1).
x2() -> binary_to_list(x1()).
x1() -> <<"very long, more than 10000 chars here.">>.