标签:from ons ble enqueue UI moved move led any
Java provides two different types/classes of Reference Objects: strong and weak. Weak Reference Objects can be further divided into soft and phantom.
Strong Reference
StringBuilder builder = new StringBuilder();
This is the default type/class of Reference Object, if not differently specified: builder
is a strong Reference Object. This kind of reference makes the referenced object not eligible for GC. That is, whenever an object is referenced by a chain of strong Reference Objects, it cannot be garbage collected.
Weak Reference
WeakReference<StringBuilder> weakBuilder = new WeakReference<StringBuilder>(builder);
Weak Reference Objects are not the default type/class of Reference Object and to be used they should be explicitly specified like in the above example. This kind of reference makes the reference object eligible for GC. That is, in case the only reference reachable for the StringBuilder object in memory is, actually, the weak reference, then the GC is allowed to garbage collect the StringBuilder object. When an object in memory is reachable only by Weak Reference Objects, it becomes automatically eligible for GC.
Soft Reference
Garbage collector can collect an object if only weak references are pointing towards it and they are eagerly collected, on the other hand Objects with SoftReference are collected when JVM absolutely needs memory.
Phantom Reference
Object which only has Phantom reference pointing them can be collected whenever Garbage Collector likes it.
WeakHashMap
Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.
ReferenceQueue
If the garbage collector discovers an object that is weakly reachable, the following occurs:
When creating non-strong reference objects we have the option of passing a reference queue as a part of the Reference constructor. As seen from the above explanation, this reference queue is a way for the GC to inform the program that a certain object is no longer reachable.
import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; public class Test { public static void main(String[] args) throws InterruptedException { SavePoint savePoint = new SavePoint("Random"); // a strong object ReferenceQueue<SavePoint> savepointQ = new ReferenceQueue<SavePoint>();// the ReferenceQueue WeakReference<SavePoint> savePointWRefernce = new WeakReference<SavePoint>(savePoint, savepointQ); System.out.println("SavePoint created as a weak ref " + savePointWRefernce); Runtime.getRuntime().gc(); System.out.println("Any weak references in Q ? " + (savepointQ.poll() != null)); savePoint = null; // the only strong reference has been removed. The heap // object is now only weakly reachable System.out.println("Now to call gc..."); Runtime.getRuntime().gc(); // the object will be cleared here - finalize will be called. System.out.println("Any weak references in Q ? " + (savepointQ.remove() != null)); System.out.println("Does the weak reference still hold the heap object ? " + (savePointWRefernce.get() != null)); System.out.println("Is the weak reference added to the ReferenceQ ? " + (savePointWRefernce.isEnqueued())); } } class SavePoint { public SavePoint(String value) { } }
The program :
As seen once the object was ready to be released it was added to the reference queue. So the reference queue is like a callback to our java program, telling us that a particular object is released from its reference and is not available to our code anymore.
As seen from above the Reference queue actually holds within it the WeakReference which lost its heap object to clean up. The WeakReference does not have any association to the memory object. The get call above returns null. Unlike with finalize when we can make the object alive again, with the ReferenceQ there is no way to reach the released java object. Reference Queues are just for References that got freed by garbage collection. They cannot be used to make alive our objects again. They can only be used to notify our code about the loss of memory objects referred to by these non- strong references.
标签:from ons ble enqueue UI moved move led any
原文地址:http://www.cnblogs.com/codingforum/p/7072383.html