End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ObjectSpace.memsize_of_all
ObjectSpace.memsize_of_all([klass]) → 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);
}
each_object_with_flags(total_i, &data);
return SIZET2NUM(data.total);
}
Return consuming memory size of all living objects in bytes.
If klass (should be Class object) is given, return the total memory size of instances of the given class.
Note that the returned size is incomplete. You need to deal with this information as only a HINT. Especially, the size of T_DATA may not be correct.
Note that this method does NOT return total malloc’ed memory size.
This method can be defined by the following Ruby code:
Example
def memsize_of_all klass = false total = 0 ObjectSpace.each_object{|e| total += ObjectSpace.memsize_of(e) if klass == false || e.kind_of?(klass) } total end
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.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0