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 8, 2021
1 parent 52d3b2c commit ee83696
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 @@ -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}.
Expand Down Expand Up @@ -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) ->
Expand Down

0 comments on commit ee83696

Please sign in to comment.