Skip to content

Instantly share code, notes, and snippets.

@rkh
Last active July 26, 2026 22:37
Show Gist options
  • Select an option

  • Save rkh/5e2732c724c2fe94d19fb07f7848e25f to your computer and use it in GitHub Desktop.

Select an option

Save rkh/5e2732c724c2fe94d19fb07f7848e25f to your computer and use it in GitHub Desktop.
Memory size of Ractor vs Thread vs Fiber

On my machine (Apple M4 Max):

  • 53 KB per ractor
  • 71 KB per thread
  • 48 KB per fiber

And performance overhead (10k simple, linear return value):

             user     system      total        real
ractor   0.783589   0.368951   1.152540 (  1.074543)
thread   0.917771   0.449474   1.367245 (  1.198391)
fiber    0.009250   0.017416   0.026666 (  0.026904)
# frozen_string_literal: true
Warning[:experimental] = false
def self.get_memory
GC.start
`ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.last.to_i
end
args = ARGV.any? ? ARGV : ["ractor", "thread", "fiber"]
args.each do |arg|
case arg
when "ractor" then factory = proc { Ractor.new { sleep } }
when "thread" then factory = proc { Thread.new { sleep } }
when "fiber" then factory = proc { Fiber.new { Fiber.yield }.resume }
else
warn "Unknown argument: #{arg}", "Usage: #{$PROGRAM_NAME} [ractor|thread|fiber]"
exit 1
end
first = get_memory
result = 1000.times.map(&factory)
last = get_memory
puts "#{(last - first) / result.size} KB per #{arg}"
end
require "benchmark"
count = 10_000
Benchmark.bmbm do |x|
x.report("ractor") { count.times { Ractor.new { true }.value } } if args.include?("ractor")
x.report("thread") { count.times { Thread.new { true }.value } } if args.include?("thread")
x.report("fiber") { count.times { Fiber.new { true }.resume } } if args.include?("fiber")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment