Module

ObjectSpace

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
Public Methods
_id2ref 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.
add_finalizer deprecated
call_finalizer deprecated
define_finalizer Adds aProc as a finalizer, to be called after obj was destroyed.
each_object 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.
finalizers deprecated
garbage_collect Initiates garbage collection, unless manually disabled.
remove_finalizer deprecated
undefine_finalizer Removes all finalizers for obj.
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.