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
ext/objspace/objspace.c
View on GitHub ↗
static VALUE
memsize_of_all_m(int argc, VALUE *argv, VALUE self)
{
struct total_data data = {0, 0};
if (argc > 0) {
rb_scan_args(argc, argv, "01", &data.klass);
if (!NIL_P(data.klass)) rb_obj_is_kind_of(Qnil, data.klass);
}
each_object_with_flags(total_i, &data);
return SIZET2NUM(data.total);
}
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.
See also
.memsize_of
.reachable_objects_from
.internal_super_of
.reachable_objects_from_root
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0