From ffda347e87b7e3266b78782ca76d4cfb8a1fe201 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 16 May 2025 13:35:55 -0400 Subject: [PATCH] Hibernate after collecting garbage in `rabbit_mgmt_gc` The `rabbit_mgmt_gc` gen_server performs garbage collections periodically. When doing so it can create potentially fairly large terms, for example by creating a set out of `rabbit_exchange:list_names/0`. With many exchanges, for example, the process memory usage can climb steadily especially when the management agent is mostly idle since `rabbit_mgmt_gc` won't hit enough reductions to cause a full-sweep GC on itself. Since the process is only active periodically (once every 2min by default) we can hibernate it to GC the terms it created. This can save a medium amount of memory in situations where there are very many pieces of metadata (exchanges, vhosts, queues, etc.). For example on an idle single-node broker with 50k exchanges, `rabbit_mgmt_gc` can hover around 50MB before being naturally GC'd. With this patch the process memory usage stays consistent between `start_gc` timer messages at around 1KB. (cherry picked from commit ce5d42a9d6118a8a9e001b250ec5a982661abb23) --- deps/rabbitmq_management_agent/src/rabbit_mgmt_gc.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/rabbitmq_management_agent/src/rabbit_mgmt_gc.erl b/deps/rabbitmq_management_agent/src/rabbit_mgmt_gc.erl index 5f6d5659a7..fe408787c1 100644 --- a/deps/rabbitmq_management_agent/src/rabbit_mgmt_gc.erl +++ b/deps/rabbitmq_management_agent/src/rabbit_mgmt_gc.erl @@ -36,7 +36,7 @@ handle_info(start_gc, State) -> gc_queues(), gc_exchanges(), gc_nodes(), - {noreply, start_timer(State)}. + {noreply, start_timer(State), hibernate}. terminate(_Reason, #state{timer = TRef}) -> _ = erlang:cancel_timer(TRef),