Part 2 · 1 chapters · ~8 min

Memory, the GVL and YJIT

The CRuby VM (YARV bytecode), the global VM lock and when it is released, YJIT, object allocation and the generational GC with compaction, memory bloat and malloc fragmentation, jemalloc, and alternative Rubies (JRuby, TruffleRuby).

3

Inside CRuby

code
RubyVM::InstructionSequence.compile("1 + 2").disasm     # YARV bytecode
ruby --yjit app.rb ; RubyVM::YJIT.enabled?              # YJIT on
GC.stat.slice(:count, :minor_gc_count, :major_gc_count, :heap_live_slots)
ObjectSpace.count_objects[:T_STRING]                    # how many strings are alive
# Gemfile: frozen string literals cut allocations
# frozen_string_literal: true
CRUBY: THE GVL, YJIT AND GC
one thread runs Ruby at a time; IO releases the lock
thread 1running Rubythread 2waiting on Postgresthread 3waiting for GVLGVLglobal VM lockYJITJIT compiler (Ruby 3.1+)GCgenerational, incremental, compaction
swipe the figure sideways, or tap expand for full screen
1/5
the GVL
CRuby has a global VM lock: only one thread executes Ruby code at a time, like Python's GIL (Python course P3).
one thread runs Ruby at a timelike Python's GIL