method

reachable_objects_from

Importance_2
v2_2_9 - Show latest stable - 0 notes - Class: ObjectSpace
reachable_objects_from(p1) public
MRI specific feature

Return all reachable objects from `obj’.

This method returns all reachable objects from `obj’.

If `obj’ has two or more references to the same object `x’, then returned array only includes one `x’ object.

If `obj’ is a non-markable (non-heap management) object such as true, false, nil, symbols and Fixnums (and Flonum) then it simply returns nil.

If `obj’ has references to an internal object, then it returns instances of ObjectSpace::InternalObjectWrapper class. This object contains a reference to an internal object and you can check the type of internal object with `type’ method.

If `obj’ is instance of ObjectSpace::InternalObjectWrapper class, then this method returns all reachable object from an internal object, which is pointed by `obj’.

With this method, you can find memory leaks.

This method is only expected to work except with C Ruby.

Example:

ObjectSpace.reachable_objects_from(['a', 'b', 'c'])
#=> [Array, 'a', 'b', 'c']

ObjectSpace.reachable_objects_from(['a', 'a', 'a'])
#=> [Array, 'a', 'a', 'a'] # all 'a' strings have different object id

ObjectSpace.reachable_objects_from([v = 'a', v, v])
#=> [Array, 'a']

ObjectSpace.reachable_objects_from(1)
#=> nil # 1 is not markable (heap managed) object
Show source
Register or log in to add new notes.