执行mnesia:dirty_select/2的高级erlang进程内存

h7wcgrx3  于 2022-12-16  发布在  Erlang
关注(0)|答案(1)|浏览(182)

我有一个mnesia表gen_saga_persist,它每1秒被一个进程查询一次。
使用的函数为mnesia:dirty_select(gen_saga_persist, [{#gen_saga_persist{id = '$1', app = undefined, partition = 33, execution_type = auto, _ = '_'}, [], ['$1']}])
现在,随着表的增长,我观察到我的进程查询这个表的内存也在增长,有人能帮我理解为什么我的进程内存在增长吗(这里是mnesia新手),因为我甚至没有把结果存储在我的进程数据或任何地方。
Mnesia表格信息:

[{access_mode,read_write},
 {active_replicas,[server@localhost]},
 {all_nodes,[server@localhost]},
 {arity,12},
 {attributes,[id,partition,app,type,subtype,name,status,
              execution_type,retry_count,update_time,controller_state]},
 {checkpoints,[]},
 {commit_work,[{index,ordered_set,
                      [{{9,ordered},{ram,#Ref<0.3472765205.1199964162.76896>}},
                       {{8,ordered},{ram,#Ref<0.3472765205.1199964162.76882>}},
                       {{7,ordered},{ram,#Ref<0.3472765205.1199964163.74776>}},
                       {{6,ordered},{ram,#Ref<0.3472765205.1199964162.76856>}},
                       {{5,ordered},{ram,#Ref<0.3472765205.1199964161.79429>}},
                       {{4,ordered},{ram,#Ref<0.3472765205.1199964161.79409>}},
                       {{3,ordered},{ram,#Ref<0.3472765205.1199964162.76810>}}]}]},
 {cookie,{{1632211298276938000,-576460752303418559,1},
          server@localhost}},
 {cstruct,{cstruct,gen_saga_persist,ordered_set,[],
                   [server@localhost],
                   [],[],0,read_write,false,
                   [{3,ordered},
                    {4,ordered},
                    {5,ordered},
                    {6,ordered},
                    {7,ordered},
                    {8,ordered},
                    {9,ordered}],
                   [],false,gen_saga_persist,
                   [id,partition,app,type,subtype,name,status,execution_type,
                    retry_count,update_time,controller_state],
                   [],[],[],
                   {{1632211298276938000,-576460752303418559,1},
                    server@localhost},
                   {{2,0},[]}}},
 {disc_copies,[server@localhost]},
 {disc_only_copies,[]},
 {external_copies,[]},
 {frag_properties,[]},
 {index,[9,8,7,6,5,4,3]},
 {index_info,{index,ordered_set,
                    [{{9,ordered},{ram,#Ref<0.3472765205.1199964162.76896>}},
                     {{8,ordered},{ram,#Ref<0.3472765205.1199964162.76882>}},
                     {{7,ordered},{ram,#Ref<0.3472765205.1199964163.74776>}},
                     {{6,ordered},{ram,#Ref<0.3472765205.1199964162.76856>}},
                     {{5,ordered},{ram,#Ref<0.3472765205.1199964161.79429>}},
                     {{4,ordered},{ram,#Ref<0.3472765205.1199964161.79409>}},
                     {{3,ordered},{ram,#Ref<0.3472765205.1199964162.76810>}}]}},
 {load_by_force,false},
 {load_node,server@localhost},
 {load_order,0},
 {load_reason,local_only},
 {local_content,false},
 {majority,false},
 {master_nodes,[]},
 {memory,2455944},
 {ram_copies,[]},
 {record_name,gen_saga_persist},
 {record_validation,{gen_saga_persist,12,ordered_set}},
 {size,7747},
 {snmp,[]},
 {storage_properties,[]},
 {storage_type,disc_copies},
 {subscribers,[]},
 {type,ordered_set},
 {user_properties,[]},
 {version,{{2,0},[]}},
 {where_to_commit,[{server@localhost,disc_copies}]},
 {where_to_read,server@localhost},
 {where_to_wlock,{[server@localhost],false}},
 {where_to_write,[server@localhost]},
 {wild_pattern,#gen_saga_persist{id = '_',partition = '_',
                                 app = '_',type = '_',subtype = '_',name = '_',status = '_',
                                 execution_type = '_',retry_count = '_',update_time = '_',
                                 controller_state = '_'}},
 {{index,3},#Ref<0.3472765205.1199964162.76810>},
 {{index,4},#Ref<0.3472765205.1199964161.79409>},
 {{index,5},#Ref<0.3472765205.1199964161.79429>},
 {{index,6},#Ref<0.3472765205.1199964162.76856>},
 {{index,7},#Ref<0.3472765205.1199964163.74776>},
 {{index,8},#Ref<0.3472765205.1199964162.76882>},
 {{index,9},#Ref<0.3472765205.1199964162.76896>}]

工艺信息:

[{current_function,{timer,sleep,1}},
 {initial_call,{bin_updates_dispatcher,poll_func,3}},
 {status,waiting},
 {message_queue_len,0},
 {links,[]},
 {dictionary,[]},
 {trap_exit,false},
 {error_handler,error_handler},
 {priority,normal},
 {group_leader,<105603.71.0>},
 {total_heap_size,4781410},
 {heap_size,1199557},
 {stack_size,6},
 {reductions,155758726},
 {garbage_collection,[{max_heap_size,#{error_logger => true,kill => true,size => 0}},
                      {min_bin_vheap_size,46422},
                      {min_heap_size,233},
                      {fullsweep_after,65535},
                      {minor_gcs,11}]},
 {suspending,[]}]

工艺功能:

poll_gen_saga_persist_table(Delay) ->
    Pid = spawn(bin_updates_dispatcher, poll_func, [Delay, gen_saga_persist,
                [{#gen_saga_persist{
                    id = '$1', app = undefined, partition = 33, execution_type = auto, _ = '_'
                }, [], ['$1']}]]),
    unlink(Pid),
    Pid.

poll_func(Delay, TableName, [{MatchSpec, Guards, Return}]) ->
    mnesia:dirty_select(TableName, [{MatchSpec, Guards, Return}]),
    timer:sleep(Delay),
    poll_func(Delay, TableName, [{MatchSpec, Guards, Return}]).
iklwldmw

iklwldmw1#

我觉得跟mneisa没有关系,是你的poll_func引起的,在self循环里一直运行。

poll_func(Delay, TableName, [{MatchSpec, Guards, Return}]) ->
    mnesia:dirty_select(TableName, [{MatchSpec, Guards, Return}]),
    timer:sleep(Delay),
    poll_func(Delay, TableName, [{MatchSpec, Guards, Return}]). <--- it can't release any memory.

可以使用timer:send_after message通过gen_server重写以下代码

Pid = spawn(bin_updates_dispatcher, poll_func, [Delay, gen_saga_persist,
                [{#gen_saga_persist{
                    id = '$1', app = undefined, partition = 33, execution_type = auto, _ = '_'
                }, [], ['$1']}]]),

并简化poll_func

poll_func(Delay, TableName, [{MatchSpec, Guards, Return}]) ->
        mnesia:dirty_select(TableName, [{MatchSpec, Guards, Return}]).

因此对于pid来说,dirty_select造成的内存在调用完成后会被释放,并在下一次循环中被重用。

相关问题