Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Shopify Engineering 13 min read Case study hardMaster

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

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.

Related posts

  • Optimizing Ruby’s Memory Layout: Variable Width Allocation

    Shopify Engineering hardMaster

    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. Heap pages grew from 16 KB to 64 KB, and compaction moves resized objects into the best-fitting pool to restore data locality. Embedded strings now go up to 615 bytes and arrays and objects up to 78 elements or instance variables. Microbenchmarks show up to 35% speedups and railsbench gains 2.1%. The change also adds GC.stat_heap, offers a build flag to disable the feature, and opens the door to MMTk-based garbage collector research.

  • Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions

    Shopify Engineering easyNovice

    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. Variable Width Allocation became the default: Ruby learns when a class needs more instance-variable slots than the fixed 40-byte object provides and allocates later instances from wider slot pools, which improves cache locality. Ruby 3.2's Object Shapes give inline caches a class-independent key based on which instance variables were set and in what order, which helps deep hierarchies like Active Record. On the tooling side, Ruby LSP brings editor features to Ruby, and Error Highlight's column-level error pointers now appear on Rails error pages, part of a broader push for developer ergonomics in 2023.

  • To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models

    Shopify Engineering mediumAdept

    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. In one Shopify production app an extra Unicorn worker cost about 427MiB versus about 257MiB for an extra thread, not the ~750MiB RSS implied, and preloading plus eager loading improves CoW efficiency. Processes still offer clean request timeouts and avoid Global VM Lock contention, which hurts latency more than commonly assumed because Ruby web requests are less I/O-heavy than believed and GC also holds the GVL. Ractors and Fibers are not drop-in replacements, and a proposed GVL instrumentation API for Ruby 3.2 would let teams measure the latency cost of threads.

  • Understanding Garbage Collection in JavaScriptCore From Scratch

    WebKit hardMaster

    JavaScriptCore's garbage collector is non-compacting, generational and mostly concurrent, with heavy use of lock-free programming. The explanation starts from a naive stop-the-world generational collector and adds pieces one at a time until it reaches JSC's real design. Along the way it covers BlockDirectory-based allocators (CompleteSubspace, PreciseAllocation, IsoSubspace), why per-object metadata is split between an inlined cellState and outlined isNew/isMarked bitvectors, and how write barriers and remembered sets keep marking correct. For incremental and concurrent collection it adds remembered-set scheduling and Space-Time Scheduler mitigations, which address up to 5x memory blowup.

  • Ruby 3.2’s YJIT is Production-Ready

    Shopify Engineering easyNovice

    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. The memory savings came from smaller metadata, garbage collection of unused machine code and lazily allocated code pages. YJIT now runs about 38% faster than the 3.2 interpreter on railsbench and supports ARM64. Deployed across Shopify's entire Storefront Renderer, it delivers 5–10% end-to-end request speedups, and its fast compilation suits continuous deployment. Planned work targets memory footprint, faster method calls via a new calling convention and inlining, and a register allocator.

The quest for software engineering knowledge.

Look:
Layout: