|
|
3.5 Special kinds of object
A few methods in Object support the creation of particular objects.
This include:
-
finalizable objects
-
weak objects (i.e. objects whose contents are not considered,
during garbage collection, when scanning the heap for live objects).
-
read-only objects (like literals found in methods)
-
fixed objects (guaranteed not to move across garbage collections)
They are:
- Method: Object makeWeak
- Marks the object so that it is considered weak in subsequent GC passes.
The garbage collector will consider dead an object which has references
only inside weak objects, and will replace references to such an
"almost-dead" object with nils.
- Method: Object addToBeFinalized
- Marks the object so that, as soon as it becomes unreferenced, its
finalize method is called. Before finalize is called,
the VM implicitly removes the objects from the list of finalizable
ones. If necessary, the finalize method can mark again
the object as finalizable, but by default finalization will only occur
once.
Note that a finalizable object is kept in memory even when it has no
references, because tricky finalizers might "resuscitate" the object;
automatic marking of the object as not to be finalized has the nice side
effect that the VM can simply delay the releasing of the memory associated
to the object, instead of being forced to waste memory even after
finalization happens.
An object must be explicitly marked as to be finalized every time the
image is loaded; that is, finalizability is not preserved by an
image save. This was done because in most cases finalization is
used together with CObject s that would be stale when the image is
loaded again, causing a segmentation violation as soon as they are accessed
by the finalization method.
- Method: Object removeToBeFinalized
- Removes the to-be-finalized mark from the object.
As I noted above, the finalize code for the object does not have to
do this explicitly.
- Method: Object finalize
- This method is called by the VM when there are no more references to
the object (or, of course, if it only has references inside weak objects).
- Method: Object isReadOnly
- This method answers whether the VM will refuse to make changes to the
objects when methods like
become: , basicAt:put: ,
and possibly at:put: too (depending on the implementation of the
method).
Note that GNU Smalltalk won't try to intercept assignments to fixed
instance variables, nor assignments via instVarAt:put: . Many
objects (Characters, nil , true , false , method
literals) are read-only by default.
- Method: Object makeReadOnly: aBoolean
- Changes the read-only or read-write status of the receiver to that
indicated by
aBoolean .
- Method: Object basicNewInFixedSpace
- Same as
#basicNew , but the object won't move across garbage
collections.
- Method: Object basicNewInFixedSpace:
- Same as
#basicNew: , but the object won't move across garbage
collections.
- Method: Object makeFixed
- Ensure that the receiver won't move across garbage collections.
This can be used either if you decide after its creation that an
object must be fixed, or if a class does not support using
#new
or #new: to create an object
Note that, although particular applications will indeed have a need for
fixed, read-only or finalizable objects, the #makeWeak primitive
is seldom needed and weak objects are normally used only indirectly,
through the so called weak collections. These are easier to use
because they provide additional functionality (for example, WeakArray
is able to determine whether an item has been garbage collected, and
WeakSet implements hash table functionality); they are:
-
WeakArray
-
WeakSet
-
WeakKeyLookupTable
-
WeakValueLookupTable
-
WeakIdentitySet
-
WeakKeyIdentityDictionary
-
WeakValueIdentityDictionary
|