Detailed reference to the four java!

What is java four references?

 

JDK1.2 version is the beginning of the introduction, the reference object is divided into four levels, we usually is the first stage of a strong reference to four kinds of learning, put it plainly, it Object o = new Object (), four were cited strong references, soft, weak, and phantom references cited, remember their order! This is because they are more sensitive to memory!

Grade: strong> Soft> weak> false

They do?

Lifecycle flexible control object to improve the chances of recovery of objects

Add code analysis theory:

What is a strong reference

Strong reference to the object is the way we usually use, but also the most used one way, keep in mind that regardless of memory tense worth mentioning, worth mentioning is insufficient, gc strong references never be recovered, even if the jvm appear (memory overflow error) OutOfMemoryError, the program stop, it will not be recovered to improve the code memory

public class StrongReferenceDemo {public static void main (String [] args) {Integer data = new Integer (4); String str = "123"; Student student = new Student (); // objects are three or more strong object reference refers to the way we usually use the object}} class Student {}

What is soft references

Objects with soft references, remember that as long as the memory is enough, we do not recover the object, but when there is insufficient memory, gc referenced objects can be seen on the recycled soft soft references are sensitive to memory, can be used to cache, and it can be combined with the queue using, if the soft reference is recovered GC, the JVM soft references will be added to the queue.

Code

public class Demo {public static void main (String [] args) {String ss = "hello"; // time "hello" has a strong reference, and a soft reference refers to a soft // generic references to the type // soft reference is insufficient memory, the object is recovered SoftReference <String> soft = new SoftReference <String> (ss); // get the object reference of soft String content = soft.get (); System.out.println ( content); ss = null; // no strong references, and then only soft references to "hello" System.gc (); // mandatory recycling System.gc (); System.gc (); content = soft.get (); // see if there is recovered}}

What is weak references?

gc memory is not found weak references, a weak reference objects will be recovered immediately, but we know that gc is a very low priority thread, so she does not immediately find and recover a weak reference objects, but remember, as long as the weak references to be found gc , regardless of the memory enough, direct recycling, simultaneously, weak references, may be used in conjunction with the queue, when recovered, it will go to the associated queue

Code

public class WeakReference {public static void main (String [] args) {String ss = "hello"; ReferenceQueue <String> queue = new ReferenceQueue <> (); java.lang.ref.WeakReference <String> weak = newjava.lang .ref.WeakReference <String> (ss, queue); System.out.println ( "now a reference to" + weak.get ()); ss = null; System.gc (); // gcReference <extends String?> poll = queue.poll (); // if null, the description is recovered if (poll = null!) {String content = poll.get (); System.out.println (content);} // see if there Be recycled}}

What is a virtual reference?

False references and soft references previous, different weak reference, it does not affect the lifetime of an object. Java.lang.ref.PhantomReference represented by the java class. If an object is associated with a virtual reference, with no reference associated with the same, they are likely to be garbage collected at any time.

It should be noted that the phantom reference must be used in association with a reference queue, when the garbage collector is ready to reclaim an object, if it is found that there is a virtual reference, this will be added to the reference phantom reference associated with the queue. Determining whether the program can reference has been added to the virtual queue references 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.

import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; public class Main {

public static void main(String[] args) {

ReferenceQueue<String> queue = new ReferenceQueue<String>(); PhantomReference<String> pr = new PhantomReference<String>(new

String("hello"), queue);

System.out.println(pr.get());

}

}

Published 682 original articles · won praise 1391 · Views 1.71 million +

Guess you like

Origin blog.csdn.net/itcast_cn/article/details/104865812