Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
ObjectSpace.reachable_objects_from_root
static VALUE
reachable_objects_from_root(VALUE self)
{
struct rofr_data data;
VALUE hash = data.categories = rb_ident_hash_new();
data.last_category = 0;
rb_objspace_reachable_objects_from_root(reachable_object_from_root_i, &data);
rb_hash_foreach(hash, collect_values_of_values, hash);
return hash;
}
Returns a hash of objects directly reachable from the VM roots, grouped by the root that reaches them.
The roots are the entry points the garbage collector starts from when it marks live objects, such as the virtual machine and the global variable table. The keys of the returned hash are strings naming each root, and each value is an array of the objects reachable from that root:
require 'objspace' reachable = ObjectSpace.reachable_objects_from_root reachable.keys # => ["vm", "global_tbl", "machine_context", "global_symbols"] reachable.values.first # => [#<Ractor:#1 running>, ...]
The returned hash compares its keys by identity, so it cannot be indexed with a string literal; iterate over it (or over its values) instead.
Any reference to an internal object is wrapped in an ObjectSpace::InternalObjectWrapper object.
This method is useful for debugging the object graph, for example when tracking down the cause of a memory leak.
This method is only expected to work with C Ruby.