Tuesday, March 13, 2012

Reference

Pakckage java.lang.ref Description

Classes in this package provides reference-object classes, which support a limited degree of interaction with the garbage collector. Specilitate GC run to take less time and maximize the chance of not getting OutOfMemoryException.

There are four kind of references possible in JAVA

  1. Strong reference - which get created when object new instance done, and reachable without any other Reference objects. Object obj=new Object();
  2. SoftReference - Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand.
    Created object in step1 is passed to create SoftReference Object and the same retrieved via SoftReference object.
    java.lang.ref.Reference softReference=new java.lang.ref.SoftReference(obj); All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError. Otherwise no constraints are placed upon the time at which a soft reference will be cleared or the order in which a set of such references to different objects will be cleared. Virtual machine implementations are, however, encouraged to bias against clearing recently-created or recently-used soft references.
  3. WeakReference - Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed.
    Weak references are most often used to implement canonicalizing mappings.
    java.lang.ref.Reference weakReference=new java.lang.ref.WeakReference(obj);
    java.util.WeakHashMap
  4. PhantomReference - Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

Notification

A program may request to be notified of changes in an object's reachability by registering an appropriate reference object with a reference queue at the time the reference object is created. Some time after the garbage collector determines that the reachability of the referent has changed to the value corresponding to the type of the reference, it will add the reference to the associated queue. At this point, the reference is considered to be enqueued. The program may remove references from a queue either by polling or by blocking until a reference becomes available.

Reachability

Going from strongest to weakest, the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:
  • An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it.
  • An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
  • An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.
  • An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
  • Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.
JavaDoc

1 comment:

Used Cat Equipment said...

The system may eliminate sources from a line either by polling or by preventing until a referrals becomes available.

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com