- Inheritance
The ObjectSpace module contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator.
ObjectSpace also provides support for object finalizers, procs that will be called when a specific object is about to be destroyed by garbage collection.
include ObjectSpace a = "A" b = "B" c = "C" define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" }) define_finalizer(a, proc {|id| puts "Finalizer two on #{id}" }) define_finalizer(b, proc {|id| puts "Finalizer three on #{id}" })
produces:
Finalizer three on 537763470 Finalizer one on 537763480 Finalizer two on 537763480
Methods
Class
Visibility | Signature |
---|---|
public | _id2ref (p1) |
public | add_finalizer (p1) |
public | call_finalizer (p1) |
public | define_finalizer (...) |
public | each_object (...) |
public | finalizers () |
public | garbage_collect () |
public | remove_finalizer (p1) |
public | undefine_finalizer (p1) |
Class Method Detail
ObjectSpace._id2ref(object_id) → an_object
Converts an object id to a reference to the object. May not be called on an object id passed as a parameter to a finalizer.
s = "I am a string" #=> "I am a string" r = ObjectSpace._id2ref(s.object_id) #=> "I am a string" r == s #=> true
add_finalizer(p1)
deprecated
call_finalizer(p1)
deprecated
ObjectSpace.define_finalizer(obj, aProc=proc())
Adds aProc as a finalizer, to be called after obj was destroyed.
ObjectSpace.each_object([module]) {|obj| ... } => fixnum
Calls the block once for each living, nonimmediate object in this Ruby process. If module is specified, calls the block for only those classes or modules that match (or are a subclass of) module. Returns the number of objects found. Immediate objects (Fixnums, Symbols true, false, and nil) are never returned. In the example below, each_object returns both the numbers we defined and several constants defined in the Math module.
a = 102.7 b = 95 # Won't be returned c = 12345678987654321 count = ObjectSpace.each_object(Numeric) {|x| p x } puts "Total count: #{count}"
produces:
12345678987654321 102.7 2.71828182845905 3.14159265358979 2.22044604925031e-16 1.7976931348623157e+308 2.2250738585072e-308 Total count: 7
finalizers()
deprecated
GC.start => nil
gc.garbage_collect => nil
ObjectSpace.garbage_collect => nil
Initiates garbage collection, unless manually disabled.
remove_finalizer(p1)
deprecated
ObjectSpace.undefine_finalizer(obj)
Removes all finalizers for obj.