1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
</copyright>
<class name="ObjectSpace" type="module">
<p/>
The <modulename>ObjectSpace</modulename> module contains a number of routines that
interact with the garbage collection facility and allow you to
traverse all living objects with an iterator.
<p/>
<modulename>ObjectSpace</modulename> also provides support for object
finalizers, procs that will be called when a specific object is
about to be destroyed by garbage collection.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ 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}" })
]]></fullcode>
include<nbsp/>ObjectSpace
<p/>
a<nbsp/>=<nbsp/>"A"
b<nbsp/>=<nbsp/>"B"
c<nbsp/>=<nbsp/>"C"
<p/>
define_finalizer(a,<nbsp/>proc<nbsp/>{|id|<nbsp/>puts<nbsp/>"Finalizer<nbsp/>one<nbsp/>on<nbsp/>#{id}"<nbsp/>})
define_finalizer(a,<nbsp/>proc<nbsp/>{|id|<nbsp/>puts<nbsp/>"Finalizer<nbsp/>two<nbsp/>on<nbsp/>#{id}"<nbsp/>})
define_finalizer(b,<nbsp/>proc<nbsp/>{|id|<nbsp/>puts<nbsp/>"Finalizer<nbsp/>three<nbsp/>on<nbsp/>#{id}"<nbsp/>})
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
0x4018d4f0
n<nbsp/>finals=>1
Finalizer<nbsp/>three<nbsp/>on<nbsp/>537684600
0x4018d504
n<nbsp/>finals=>0
Finalizer<nbsp/>one<nbsp/>on<nbsp/>537684610
n<nbsp/>finals=>0
Finalizer<nbsp/>two<nbsp/>on<nbsp/>537684610
</alltt>
</codefragment>
<p/>
<methods type="class">
<p/>
<method name="_id2ref" ref="_id2ref">
<callseq>
ObjectSpace._id2ref( <obj>anId</obj> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
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.
<p/>
<codefragment>
<fullcode><![CDATA[ s = "I am a string"
r = ObjectSpace._id2ref(s.id)
r == s
]]></fullcode><rubycode>
<tr>
<td><tt>s<nbsp/>=<nbsp/>"I<nbsp/>am<nbsp/>a<nbsp/>string"</tt></td>
<td>»</td>
<td><tt>"I<nbsp/>am<nbsp/>a<nbsp/>string"</tt></td>
</tr>
<tr>
<td><tt>r<nbsp/>=<nbsp/>ObjectSpace._id2ref(s.id)</tt></td>
<td>»</td>
<td><tt>"I<nbsp/>am<nbsp/>a<nbsp/>string"</tt></td>
</tr>
<tr>
<td><tt>r<nbsp/>==<nbsp/>s</tt></td>
<td>»</td>
<td><tt>true</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="define_finalizer" ref="define_finalizer">
<callseq>
ObjectSpace.define_finalizer(
<obj>anObject</obj>, <obj>aProc</obj>=proc()
)
</callseq>
<desc>
<p/>
Adds <obj>aProc</obj> as a finalizer, to be called when <obj>anObject</obj>
is about to be destroyed.
<p/>
</desc>
</method>
<p/>
<method name="each_object" ref="each_object">
<callseq>
ObjectSpace.each_object( <opt>
<obj>aClassOrMod</obj></opt> )
<block>{| anObj | <blockbody>block</blockbody> }</block>
<returns><obj>aFixnum</obj></returns>
</callseq>
<desc>
<p/>
Calls the block once for each living, nonimmediate
object in this Ruby process.
If <obj>aClassOrMod</obj> is specified, calls the block for only those
classes or modules that match (or are a subclass of)
<obj>aClassOrMod</obj>.
Returns the number of objects found.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ a = 102.7
b = 95
ObjectSpace.each_object(Numeric) {|x| p x }
print "Total count: ", ObjectSpace.each_object {} ,"\n"
]]></fullcode>
a<nbsp/>=<nbsp/>102.7
b<nbsp/>=<nbsp/>95
ObjectSpace.each_object(Numeric)<nbsp/>{|x|<nbsp/>p<nbsp/>x<nbsp/>}
print<nbsp/>"Total<nbsp/>count:<nbsp/>",<nbsp/>ObjectSpace.each_object<nbsp/>{}<nbsp/>,"\n"
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
102.7
2.718281828
3.141592654
Total<nbsp/>count:<nbsp/>376
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="garbage_collect" ref="garbage_collect">
<callseq>
ObjectSpace.garbage_collect
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Initiates garbage collection (see module <modulename>GC</modulename> on page 414).
<p/>
</desc>
</method>
<p/>
<method name="undefine_finalizer" ref="undefine_finalizer">
<callseq>
ObjectSpace.undefine_finalizer(
<obj>anObject</obj> )
</callseq>
<desc>
<p/>
Removes all finalizers for <obj>anObject</obj>.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|