Skip to content

Commit

Permalink
Optimise garbage collection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NelsonVides committed Dec 12, 2021
1 parent e376602 commit 92a3089
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/mongoose_batch_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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}.
Expand Down Expand Up @@ -102,7 +104,7 @@ run_flush(State = #state{flush_interval_tref = TRef}) ->
cancel_and_flush_timer(TRef),
?LOG_DEBUG(log_fields(State, #{what => batch_worker_flush})),
NewState = do_run_flush(State#state{flush_interval_tref = undefined}),
erlang:garbage_collect(),
erlang:garbage_collect(self(), [{async, asynchronous_gc_triggered}, {type, major}]),
NewState.

cancel_and_flush_timer(undefined) ->
Expand Down

0 comments on commit 92a3089

Please sign in to comment.