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.count_objects_size

(?::Hash[::Symbol, ::Integer] result_hash) -> ::Hash[::Symbol, ::Integer]
ObjectSpace.count_objects_size(result_hash = {}) → result_hash

Counts objects size (in bytes) for each type.

Note that the returned size may not be accurate, so it should only be used as a hint. Specifically, the size for T_DATA may be inaccurate because these are custom objects defined in Ruby and native extensions and so they may not accurately report their memory size.

It returns a hash that looks like:

{TOTAL: 1461154, T_CLASS: 158280, T_MODULE: 20672, T_STRING: 527249, ...}

The contents of the returned hash are implementation specific and may be changed in future versions without notice.

If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid the probe effect.

This method is only expected to work with C Ruby.