May 27, 2026

How to get the amount of memory allocated to a Ruby object

require 'objspace'

str = "Hello, world!"
ObjectSpace.memsize_of(str)
# => 40 bytes