Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

ObjectSpace.memsize_of_all

(?::Class) -> ::Integer
ObjectSpace.memsize_of_all(klass = nil) → integer

Returns the total memory size of all living objects in bytes.

Example
ObjectSpace.memsize_of_all # => 12502001

If klass is given (which must be a Class or Module), returns the total memory size of objects whose class is, or is a subclass, of klass.

Example
class MyClass; end
ObjectSpace.memsize_of_all(MyClass) # => 0
o = MyClass.new
ObjectSpace.memsize_of_all(MyClass) # => 40

Note that the value returned may be an underestimate of the actual amount of memory used. Therefore, the value returned should only be used as a hint, rather than a source of truth. In particular, the size of T_DATA objects may not be correct.

This method is only expected to work with C Ruby.