Strong reference in JAVA (StrongReference), soft references (SoftReference), weak reference (WeakReference) and ghost reference (the PhantomReference)

Strong references:

As long as reference exists, the garbage collector never recovered
Object obj = new Object ();
// can be obtained as the object corresponding obj.equels (new Object ()) directly through obj;
and such object obj on the back of new Object strong reference, this reference only when obj is released, the object will be freed, which is encoded in the form we often are used.

 

Soft references:

Reference not necessarily, be recovered before running out of memory may be implemented by the following code
Object obj = new new Object ();
the SoftReference <Object> the SoftReference new new SF = <Object> (obj);
obj = null;
sf.get (); // sometimes return null
at this time is a reference to a soft sf obj to be taken by sf.get () method to the object, of course, when the object is marked as a subject in need of recovery, null is returned;
soft references main user cache to achieve similar functionality, reference values in memory sufficiently directly through the soft, without having to rush from the true source query data, increase speed; when there is insufficient memory, the cache is automatically deleted this part of the data from the real source of these queries data.

 

Weak references:

The recovery of the second garbage collection may be implemented in the following code
Object obj = new new Object ();
WeakReference <Object> WeakReference new new WF = <Object> (obj);
obj = null;
wf.get (); // sometimes returns null
wf.isEnQueued (); // return the garbage collector is marked for the upcoming waste recovery
weak reference is recovered in the second garbage collection in a short time by taking weak references corresponding to data to be taken, when executed the second time garbage collection will return null.
Weak references are mainly used to monitor whether the object has been marked as garbage collector soon recovered waste may return the object is marked by the garbage collector isEnQueued method weak references.

 

False quote:

When the garbage collected, the object should be taken to reference values, may be implemented by the following code
Object obj = new new Object ();
the PhantomReference <Object> the PhantomReference new new PF = <Object> (obj);
obj = null;
pf.get () ; // always return null
pf.isEnQueued (); // returns whether the memory has been deleted from
the virtual reference is a time when garbage collection will be recycled forever to get through the get method of a phantom reference data is null, therefore, known as the specter of reference.
Phantom reference is mainly used to detect whether an object has been removed from memory.

 

A recent study Java virtual machine, having problems cited in this reference summary:

Original Address: http: //blog.csdn.net/coding_or_coded/article/details/6603549

strong, soft, weak and imaginary objects referenced
in previous versions of JDK 1.2, if an object is not referenced by any variable, then the program this object is no longer available. That is, only the object is accessible (reachable), the program can use it. As of JDK version 1.2, the reference to the object is divided into four levels, so that the program can be more flexible control of the life cycle of the object. The four levels in order were: strong references, soft, weak, and phantom references cited.

 

⑴ strong reference (StrongReference)
strong reference is the most commonly used reference. If an object has a strong reference, and that the garbage collector will never recover it. When the memory space is insufficient, Java Virtual Machine would rather throw OutOfMemoryError errors, abnormal termination of the program, it will not recover by random objects that have strong references to solve the problem of insufficient memory. ps: strong reference in fact is what we usually A a = new A () it means.

⑵ soft references (SoftReference)
If an object has only soft references, the memory space is sufficient, the garbage collector does not reclaim it; if the memory space is insufficient, it will reclaim the memory of these objects. As long as the garbage collector does not reclaim it, the object can be used by the program. Soft references used to implement memory-sensitive cache (example given below).
Soft references can be a reference queue (ReferenceQueue) used in combination, if the soft reference object referenced by the garbage collector, Java virtual machine will be added to this soft references cited associated queue.

⑶ weak reference (WeakReference)
weak references and soft references that difference: only a weak reference objects have more short life cycle. In the process of the garbage collector thread scans memory area under its jurisdiction, once the objects found only a weak reference, regardless of the current memory space is sufficient or not, will reclaim its memory. However, the garbage collector is a low priority thread, and therefore will not necessarily quickly find objects that have only a weak reference.
Weak references can queue and a reference (the ReferenceQueue) used in combination, if the object referenced by the weak reference is garbage, Java virtual machine will be added to this reference weak reference associated queue.

⑷ virtual reference (PhantomReference)
"Virtual Reference" by definition, is non-existent, and several other references are different, the life cycle of virtual reference and does not determine object. If an object holds only a phantom reference, and then it does not have any references, they are likely to be garbage collected at any time.
Phantom reference primarily used to track objects recovered garbage collector activity. With reference to a virtual reference difference between the soft and weak reference is that: the virtual reference and reference must queue (the ReferenceQueue) used in combination. When the garbage collector is ready recovery of an object, it is found that if there is a virtual reference, will be recovered before the memory, this reference is added to the virtual queue associated with a reference.

ReferenceQueue queue = new ReferenceQueue ();

PR = the PhantomReference  new new  the PhantomReference (Object, Queue); 

the program can be determined by reference to whether the queue has been added to the virtual reference, to see if the referenced object is to be garbage collected. If the program finds a phantom reference has been added to the reference queue, then we can take the necessary action before it is recycled in memory referenced object.
Use soft references to build sensitive data cache
1 Why do I need to use soft references

First, let's look at an example of an employee information inquiry system. We will use a Java stored employee information inquiry system query language in the disk file or database employee personnel files of information. As a user, entirely possible we need to go back to see a few minutes or even seconds before Viewed employee file information (Similarly, we often use the "Back" button in the browser when the WEB page). Then we usually have two program implementation: one is the past viewed the employee information stored in memory, each storage lifecycle Java objects employee profile information throughout the application always; the other is when the user starts viewing the file information of other employees when the store employee file information currently viewed Java object references the end of that garbage collection thread can reclaim the memory space they occupy, when the user again need to browse the employee's file time information, information to reconstruct the employee. Obviously, the first implementation will result in a waste of a lot of memory, but defects in the second implementation is that even if the object has not been garbage collection thread garbage collection, contains the employee profile information is still well preserved in memory, and the application to reconstruct an object. We know that access to disk files, access network resources, such as database query operations are important factors that affect the execution performance of the application, if they can retrieve Java objects that have not yet been recovered reference, will reduce unnecessary visits, greatly improving the program run faster.

2 If you are using soft references
SoftReference is characterized by its soft to hold an instance of a Java object reference, the soft references exist without prejudice to the recovery of the garbage collection thread Java objects. That is, once the soft SoftReference save for a Java object reference after () method returns the Java object get garbage in the thread before the Java object recovery, SoftReference classes provide strong references. In addition, once the garbage recycling thread Java objects, get () method will return null.
Look at the code below:

MyObject aRef = new
MyObject();

ASoftRef = SoftReference new new  SoftReference (AREF); 
In this case, the MyObject objects, there are two reference paths, a soft SoftReference from a reference to the object from a reference variable aReference strong, so the object is the MyObject strongly reachable objects.
Immediately, we can end this aReference strong reference MyObject instance:

aRef = null;

Since then, the MyObject objects can become soft and objects. If the garbage collector thread memory garbage collection, and not because there is a reference to the object and SoftReference always retains the object. Java Virtual Machine garbage collection thread to be soft and objects, and other general Java objects are treated differently: can clean up the soft and the object is garbage collection thread in accordance with its specific algorithm determined in accordance with the memory requirements. In other words, throw garbage collection thread will be recovered and soft objects before OutOfMemoryError in a virtual machine, and virtual machine software can recover and priority target for a long time idle as possible for those who have just built or recently used "new "soft anti objects may be retained as a virtual machine. Before recycling these objects, we can:
the MyObject anotherRef = (the MyObject) aSoftRef.get (); 
to regain a strong reference to the instance. But after recycling, call the get () method can only get a null.

3 Use ReferenceQueue cleared lost SoftReference soft reference object
as a Java object, the object SoftReference addition to saving soft references particularity outside, also has a general Java objects. So, when the soft and the object can be recycled, although this SoftReference get object () method returns null, but this is no longer the object of value SoftReference exist, the need for a proper clearance mechanisms, to avoid memory brought a large number of objects SoftReference leakage. In java.lang.ref package also provides ReferenceQueue. If SoftReference when creating objects using a ReferenceQueue object as a parameter to the constructor SoftReference, such as:

ReferenceQueue queue = new
ReferenceQueue();

The SoftReference
ref = new new
the SoftReference (aMyObject, Queue); 
then when the soft reference while this SoftReference aMyOhject recovered garbage collector, SoftReference ref objects are referenced by the inclusion of a strong ReferenceQueue. In other words, the object is stored in ReferenceQueue Reference object, but the object it has lost Reference soft reference object. In addition ReferenceQueue can be seen from the name, it is a queue, when we call it the poll () method, if the queue is not empty queue, the queue will be returned in front of the Reference object.
At any time, we can call the poll ReferenceQueue () method to check whether there is non-mandatory and that concern may be recovered. If the queue is empty, it returns a null, otherwise the method returns to the front of a queue Reference object. Using this method, we can examine the object which SoftReference soft references has been recovered. So we can put these objects lose SoftReference soft reference objects removed. Common ways are:

SoftReference ref = null;

while ((ref = (EmployeeRef) q.poll()) != null) {

// Clear ref

}

Guess you like

Origin www.cnblogs.com/lgjava/p/10931204.html