Four ways JAVA object references

Reference Objecto = newObject ();

This O, we can call the object referenced the newObject () we can call creates an object instance in memory.

When write o = null, but no longer points to the object instance represents o the object heap, this does not mean the absence of the object instance.


 A strong reference refers to the prevalence in the program code, similar to "Objectobj = newObject ()" of such a reference, as long as there are strong references, the garbage collector will never recover lost object instance being referenced. 


Soft references are also used to describe some, but not with the necessary objects. For soft references associated with an object, the system will take place before the memory overflow exception, these objects will be listed as examples within the scope of a second recycling recovered. If the recovery has not enough memory, memory overflow exception will be thrown. After JDK 1.2, it provides SoftReference classes to implement soft references. 


Weak references are non-essential to describe the object, but its intensity is weaker than the reference number of the soft, weak references are associated to the object instance can only survive until the next garbage collection occurs. When the garbage collector job, regardless of the adequacy of current memory will only recover lost object instance is associated with a weak reference. After JDK1.2, it provides WeakReference classes to implement a weak reference. 


Virtual reference also known as ghost or phantom cited references, it is the weakest one reference relationship. The presence of an object instance is a virtual reference, will not hurt its lifetime, can not be achieved by a phantom reference object instance. The sole purpose of setting an imaginary reference target is associated with a system to receive a notification when the object instance is recovered collector. After JDK1.2, provided PhantomReference classes to implement virtual reference.

Published 18 original articles · won praise 4 · Views 142

Guess you like

Origin blog.csdn.net/weixin_42081445/article/details/104828860