Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Ruby Programming
Previous Page Home Next Page
module ObjectSpace

Index:

_id2ref define_finalizer each_object garbage_collect undefine_finalizer


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 537767360
Finalizer one on 537767510
Finalizer two on 537767510

class methods
_id2ref ObjectSpace._id2ref( anId ) -> anObject

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.id) "I am a string"
r == s true

define_finalizer ObjectSpace.define_finalizer( anObject, aProc=proc() )

Adds aProc as a finalizer, to be called when anObject is about to be destroyed.

each_object ObjectSpace.each_object( [ aClassOrMod ] ) {| anObj | block } -> aFixnum

Calls the block once for each living, nonimmediate object in this Ruby process. If aClassOrMod is specified, calls the block for only those classes or modules that match (or are a subclass of) aClassOrMod. Returns the number of objects found.

a = 102.7
b = 95
ObjectSpace.each_object(Numeric) {|x| p x }
print "Total count: ", ObjectSpace.each_object {} ,"\n"
produces:
102.7
2.718281828
3.141592654
Total count: 372

garbage_collect ObjectSpace.garbage_collect -> nil

Initiates garbage collection (see module GC on page 410).

undefine_finalizer ObjectSpace.undefine_finalizer( anObject )

Removes all finalizers for anObject.


Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire