From ee83696f6adcc7a261e845ead5caf31418ef314c Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Wed, 8 Dec 2021 18:38:07 +0100 Subject: [PATCH] Optimise garbage collection Note that running garbage collection within a function will not collect the references this function has, so the queue will actually not be cleared after being flushed as it was expected. Instead, we use the option `async` for the GC. This will make this process end its reductions, and once it has been preempted, it will be scheduled for garbage collection, and thereafter, a message will be delivered notifying him of so. --- src/mongoose_batch_worker.erl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mongoose_batch_worker.erl b/src/mongoose_batch_worker.erl index 932bad4a029..08d5b256491 100644 --- a/src/mongoose_batch_worker.erl +++ b/src/mongoose_batch_worker.erl @@ -53,6 +53,8 @@ handle_cast(Msg, State) -> -spec handle_info(flush | term(), state()) -> {noreply, state()}. handle_info(flush, State) -> {noreply, run_flush(State)}; +handle_info({garbage_collect, asynchronous_gc_triggered, true}, State) -> + {noreply, State}; handle_info(Msg, State) -> ?UNEXPECTED_INFO(Msg), {noreply, State}. @@ -100,7 +102,7 @@ run_flush(State = #state{flush_queue = Queue, flush_interval_tref = TRef}) -> ?LOG_DEBUG(log_fields(State, #{what => worker_flush})), NewState = do_run_flush(State#state{flush_queue = Queue, flush_interval_tref = undefined}), - erlang:garbage_collect(), + erlang:garbage_collect(self(), [{async, asynchronous_gc_triggered}, {type, major}]), NewState. cancel_and_flush_timer(undefined) ->