Garbage collection

Garbage collection (GC) finds unused data objects and reclaims that memory space for use by another process. The New Relic Ruby agent can collect information about how much time is spent in garbage collection for applications running on MRI 1.9.2 or higher or Ruby Enterprise Edition, but you must explicitly enable this feature in your application.

Enabling garbage collection instrumentation

To activate the feature, add the appropriate call in your application initialization.

  • MRI 1.9.2 or higher: GC::Profiler.enable
  • Ruby Enterprise Edition: GC.enable_stats

For Rails applications, you can either add this call to an initializer in config/initializers, or add it directly to your config/application.rb file.

Viewing app-wide GC statistics

To view overall garbage collection statistics:

  1. From the New Relic menu bar, select APM > Applications > (selected app) > Monitoring > Overview.
  2. From the APM Overview page, look for GC Execution statistics on the Web transactions time chart.
screen-overview-with-garbage-collection
Applications > (selected app) > Monitoring > Overview: On the Web transactions time chart, garbage collection statistics are labeled GC Execution.

Viewing detailed GC metrics

To view garbage collection metrics on a per-transaction basis, select APM > Applications > (selected app) > Transactions > (selected transaction). You can then view the amount of time spent in GC, and the average number of times GC is called for each transaction.

screen ruby gc select transaction.png
APM > Applications > (selected app) > Transactions > (selected transaction): Select an individual transaction to view detailed GC metrics, including average time and average number of calls.

Detailed information includes:

Garbage collection details Comments
Trend of garbage collection time Garbage collection usually takes a small amount of time, and therefore the trend line may not be visible in your charts. To view the trend in garbage collection time, navigate to the overview chart or to detailed transaction metrics. Then, hide every chart label except GC Execution by selecting each label.
Average number of calls To view the average number of garbage collection calls for each controller action: From your APM Overview page, select Transaction. Then select a controller action and view the App Server Breakdown.

Background information about garbage collection


[video link] Chris Kelly of New Relic presents "Down the rb_newobj() Rabbit Hole: Garbage Collection in Ruby" at Ruby Australia on Vimeo (less than 40 minutes).

For more help

Additional documentation resources include:

Join the discussion about Ruby in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.