Garbage Collection

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.

Ruby gem version 3.1.4 or newer is required for Garbage Collection profiling.

When the Ruby Garbage Collection profiler is enabled AppSignal will automatically collect metrics for its Ruby VM magic 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.

GC::Profiler.enable

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

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.

Need more help?

Contact us and speak directly with the engineers working on AppSignal. They will help you get set up, tweak your code and make sure you get the most out of using AppSignal.

Contact us

Start a trial - 30 days free

AppSignal is a great way to monitor your Ruby, Elixir & Node.js applications. Works great with Rails, Phoenix, Express and other frameworks, with support for background jobs too. Let's improve your apps together.

Start a trial