Garbage Collection

This feature requires AppSignal for Ruby version 3.1.4 or higher.

The Ruby VM (Virtual Machine) uses Garbage Collection (GC) to prevent it from running out of process memory. Garbage Collection helps prevent your application from suffering memory outages due to running out of memory.

This processes of cleaning up unused memory in Ruby apps can happen at any time. To get more insights into this you can use Ruby's Garbage Collection profiler.

Profiler

We do not recommend enabling the Garbage Collection profiler in production environments, at least not for extended periods of time. The profiler's additional overhead may negatively impact your application's performance.

When the Ruby Garbage Collection profiler is enabled AppSignal will automatically collect metrics for its Ruby VM automated dashboard. The "GC time" graph will show how long the Ruby VM took to clean up the unused memory between measurements. This graph will spike if it suddenly has to process a lot more memory, such as after a high memory usage task or request for example. The GC profiler is disabled by default, so no metrics are reported in this graph.

To enable the GC profiler, add the code shown below to your application. The profiler can be enabled at any time in the application, such as on boot or in preparation of memory intensive operations.

Once the GC profiler is enabled, the "GC time" graph will start showing metrics after at least two minutes. This metric is reported by our minutely probes system and new measurements are made every minute.

ruby
GC::Profiler.enable

To temporarily enable the GC profiler, make sure to disable the profiler afterwards.

ruby
GC::Profiler.enable # Do memory intensive operation GC::Profiler.disable

For the Ruby gem to report any duration over zero for the garbage collection time, the profiler must be enabled for more than two minutes. This is the initial time the Ruby gem needs to measure the garbage collection time. We recommend enabling GC profiling for longer than the minimum of two minutes to ensure we have complete measurements required to compare and graph.