---
title: "Adventures in Garbage Collection: Improving GC Performance in our Massive Monolith"
description: "Shopify cut Ruby garbage-collection tail latency in its monolith by finding why major GCs were triggered, via GC.latest_gc_info(:major_by), and removing each cause in turn."
url: "https://swe.quest/articles/adventures-in-garbage-collection-improving-gc-performance-in-our-massive-7c8cc3e83e915743"
canonical: "https://shopify.engineering/adventures-in-garbage-collection"
source: "Shopify Engineering"
published: "2023-04-06"
difficulty: "hard"
kind: "Case study"
reading_minutes: 13
tags: ["memory-management","ruby","performance","runtimes"]
---

# Adventures in Garbage Collection: Improving GC Performance in our Massive Monolith

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2023-04-06, 13 min read, Case study. Original: [https://shopify.engineering/adventures-in-garbage-collection](https://shopify.engineering/adventures-in-garbage-collection)

## Summary

Shopify cut Ruby garbage-collection tail latency in its monolith by finding why major GCs were triggered, via GC.latest\_gc\_info(:major\_by), and removing each cause in turn. Major marks took over 4 seconds, and most were triggered by the oldmalloc limit, so the team effectively disabled it. They then preallocated slots per size pool for Ruby 3.2's variable width allocation, since deploys every 30 minutes kept processes in warm-up. They also added write barriers to Ruby core types and gems, cutting shady objects from about 160,000 to about 3,000. Finally, out-of-band major GC every 128–512 requests cut p99.9/p99.99 GC time by about 10x and nearly eliminated GC pauses for median requests, with the warning never to tune GC without first measuring the impact.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)

## Prerequisites

- Ruby object model ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- generational garbage collection ([easy posts on Memory management](https://swe.quest/tags/memory-management.md?difficulty=easy))
- write barriers

## Keywords

Ruby 3.2, Ruby GC, variable width allocation, out-of-band GC, write barriers, google-protobuf, ffi

## Related posts

- [Optimizing Ruby’s Memory Layout: Variable Width Allocation](https://swe.quest/articles/optimizing-ruby-s-memory-layout-variable-width-allocation-7db49cee151f4d44.md): Shopify Engineering · 2022-12-25 · Hard · 13 min read · Memory management, Ruby, Runtimes & VMs, Performance engineering
  Ruby 3.2's Variable Width Allocation replaces CRuby's single 40-byte RVALUE slot with five size pools (40, 80, 160, 320 and 640 bytes) so classes, strings, arrays and objects can store their data adjacent to the object instead of in separate malloc'd memory.
- [Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions](https://swe.quest/articles/year-in-review-2022-tenderlove-s-ruby-and-rails-reflections-and-predictions-d873d71ac3e91545.md): Shopify Engineering · 2022-12-22 · Easy · 8 min read · Ruby, Runtimes & VMs, Memory management, Developer tools, Compilers
  Ruby's 2022 highlights include YJIT's rewrite in Rust and its new ARM support, so \`ruby --yjit\` now runs on Apple Silicon development machines as well as production.
- [To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models](https://swe.quest/articles/to-thread-or-not-to-thread-an-in-depth-look-at-ruby-s-execution-models-18ca691643d5cc44.md): Shopify Engineering · 2022-05-31 · Medium · 15 min read · Ruby, Concurrency, Memory management, Performance engineering, Runtimes & VMs
  Threaded Ruby servers like Puma and Sidekiq save memory over forking servers, but the gap is smaller than RSS suggests once copy-on-write sharing is measured properly with PSS and /proc/$PID/smaps\_rollup.
- [Understanding Garbage Collection in JavaScriptCore From Scratch](https://swe.quest/articles/understanding-garbage-collection-in-javascriptcore-from-scratch-6bc19cc2aad39444.md): WebKit · 2022-07-29 · Hard · 52 min read · Memory management, Runtimes & VMs, JavaScript, Concurrency
  JavaScriptCore's garbage collector is non-compacting, generational and mostly concurrent, with heavy use of lock-free programming.
- [Ruby 3.2’s YJIT is Production-Ready](https://swe.quest/articles/ruby-3-2-s-yjit-is-production-ready-05f17a68a56ea05c.md): Shopify Engineering · 2023-01-17 · Easy · 10 min read · Ruby, Compilers, Runtimes & VMs, Performance engineering, Rust
  YJIT, Shopify's JIT compiler for CRuby, became production-ready in Ruby 3.2 after a port from C99 to Rust and a cut in memory overhead to about a third of Ruby 3.1 levels.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
