Java Reference
In-Depth Information
The memory occupied by an unreachable object is reclaimed in two phases. The first phase, called finalization,
is an action automatically performed on an unreachable object before the memory used by the object is reclaimed by
the garbage collector. The block of code that contains the action to be performed is known as a finalizer. A finalizer
is implemented using the finalize() method of the object. In the finalize() method, the unreachable object may
resurrect itself by storing its reference in a reachable object. In the second phase, if the object is still unreachable, the
memory occupied by the object is reclaimed.
At times, you may want to use memory-sensitive objects, which are fine to be kept in memory if enough
memory is available. However, if the application runs low in memory, it would be fine to reclaim those objects.
Typically, objects cached for a better performance fall into this category of objects. Java provides SoftReference ,
WeakReference , and PhantomReference classes in the java.lang.ref package to work with such memory-sensitive
objects.
Search WWH ::




Custom Search