Hi All.
我这几天在项目中遇到一个封装DB进程的gen_server 进程特别消耗内存。每个进程大约会占用300~450M内存. 如果是10个这样进程内存一下在消耗接近4G。如果调用erlang:garbage_collect/1这些内存会释放掉的。推断,这些内存已经不再使用只是垃圾回收器没有回收而已(这只我的推断,求赐教),合适触发回收呢?我一直没弄明白。
我为了进一步确认问题。我把问题程序做简化为[1],再次测试,内存一直不回收(时间超出1小时),。
测试步骤:
1、
R变量存储的是19489条记录
(如[{"author_id","users-a9KsYTIRdrq3iuAywiLRE2BGd3Y"},{"timestamp","2013-07-17 10:47:36"},{"time_int",63541277256},{"title",[233,155,133,229,133,184,231,139,172,229,136,155,232,136,170,230,181,183,229,164,169,230,150,135,229,143,176,232,133,149,232,161,168]},{"id","info-137429256786232"}])
2、得到进程Pid
3、发送数据之前进制状态
{memory,2656}
[{current_function,{test4,loop,0}},
{initial_call,{test4,loop,0}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.31.0>},
{total_heap_size,233},
{heap_size,233},
{stack_size,1},
{reductions,1},
{garbage_collection,[{min_bin_vheap_size,46368},
{min_heap_size,233},
{fullsweep_after,0},
{minor_gcs,0}]},
{suspending,[]}]
4、 发送R数据
{<0.52.0>,
{ok,[{1,
[[{"author_id","users-a9KsYTIRdrq3iuAywiLRE2BGd3Y"},
{"timestamp","2013-07-17 10......................
5、再次查看状态,内存有了明显的增加
{memory,51356672}
[{current_function,{test4,loop,0}},
{initial_call,{test4,loop,0}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.31.0>},
{total_heap_size,6419485},
{heap_size,6419485},
{stack_size,1},
{reductions,2002},
{garbage_collection,[{min_bin_vheap_size,46368},
{min_heap_size,233},
{fullsweep_after,0},
{minor_gcs,0}]},
{suspending,[]}]
6、再次发送R数据
7、内存使用继续增加
{memory,125381272}
[{current_function,{test4,loop,0}},
{initial_call,{test4,loop,0}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.31.0>},
{total_heap_size,15672560},
{heap_size,15672560},
{stack_size,1},
{reductions,4003},
{garbage_collection,[{min_bin_vheap_size,46368},
{min_heap_size,233},
{fullsweep_after,0},
{minor_gcs,0}]},
{suspending,[]}]
目前很困惑内存为什么会一直不断增加,继续发数据内存还会继续增加,请赐教解决方法。
我的环境是:
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]和
Linux jason-lxw 3.2.0-55-generic #85-Ubuntu SMP Wed Oct 2 12:29:27 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[1]代码:
1 -module(test4).
2 -export([get_pid/0,
3 get_data/1,
4 get_pids/1,
5 loop/0]).
6
7 get_pids(L) ->
8 get_pids(0, L).
9
10 get_pids(0, L) ->
11 L;
12 get_pids(N, R) ->
13 NewR = [get_pid()|R],
14 get_pids(N-1, NewR).
15
16 get_pid()->
17 spawn_opt(?MODULE, loop, [], [{fullsweep_after,0}]).
18 %spawn(?MODULE, loop, []).
19
20 get_data(Pid) ->
21 Pid ! {self(), get}.
22
23 loop() ->
24 receive
25 {From, Data} ->
26 From ! Data,
27 loop()
28 end.
--
只为成功找方法,不为失败找理由