From f4081b58851536fbd1c603c9d104fc8cdba90f3f Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Fri, 17 Jan 2020 18:25:37 +0800 Subject: [PATCH] Tune the global GC --- src/emqx_global_gc.erl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/emqx_global_gc.erl b/src/emqx_global_gc.erl index a3be46a3d..1f9978dc8 100644 --- a/src/emqx_global_gc.erl +++ b/src/emqx_global_gc.erl @@ -58,7 +58,7 @@ init([]) -> {ok, ensure_timer(#{timer => undefined})}. handle_call(run, _From, State) -> - {Time, _} = timer:tc(fun run_gc/0), + {Time, ok} = timer:tc(fun run_gc/0), {reply, {ok, Time div 1000}, State, hibernate}; handle_call(_Req, _From, State) -> @@ -68,7 +68,7 @@ handle_cast(_Msg, State) -> {noreply, State}. handle_info({timeout, TRef, run}, State = #{timer := TRef}) -> - run_gc(), + ok = run_gc(), {noreply, ensure_timer(State), hibernate}; handle_info(_Info, State) -> @@ -91,7 +91,12 @@ ensure_timer(State) -> State#{timer := TRef} end. -run_gc() -> - [garbage_collect(P) || P <- processes(), - {status, waiting} == process_info(P, status)]. +run_gc() -> lists:foreach(fun do_gc/1, processes()). + +do_gc(Pid) -> + is_waiting(Pid) andalso garbage_collect(Pid, [{type, 'minor'}]). + +-compile({inline, [is_waiting/1]}). +is_waiting(Pid) -> + {status, waiting} == process_info(Pid, status).