There are four kinds of references in Java: Strong reference, soft reference, weak reference, virtual reference;
The main difference is whether the garbage collection is recycled:
1. Strong references
Use the most common references. If an object has a strong reference, it is similar to essential necessities, and the garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.
2. Soft Reference (SoftReference)
If an object has only soft references, it is similar to a living thing that can be used. If the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is insufficient, the memory of those objects is reclaimed . The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches.
A soft reference can be used in conjunction with a reference queue (Referencequeue), and if the object referenced by the soft reference is garbage collected, the Java Virtual machine will add the soft reference to the reference queue associated with it.
3. Weak references (WeakReference)
If an object has only weak references, it is similar to a living thing that can be used. The difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, it is not necessarily quick to discover objects that have only weak references.
4. Virtual Reference
Also known as a phantom reference or phantom Reference, a virtual reference does not affect the life cycle of an object, nor can it obtain an instance of an object through a virtual reference, only to receive a system notification when a GC occurs.
Test code:
Packagecom.pt;Importjava.lang.ref.SoftReference;Importjava.lang.ref.WeakReference;Importorg.junit.Test; Public classreftest {@Test Public voidTeststrongref () {User User=NewUser ("Pan"); User Strongref=user; User=NULL; System.GC (); Try{Thread.Sleep (1000); } Catch(Exception e) {//Todo:handle Exception} System.out.println ("Strong:" +strongref.getusername ()); //------test found that the string was not recycled------// {//String str = "Hello";//String strongref = str; //Strong References//str = null;//System.GC (); //Garbage Collection//try {//Thread.Sleep (+);//} catch (Exception e) {// //Todo:handle Exception// }//System.out.println ("strong:" + strongref);// }} @Test Public voidTestweakref () {User User=NewUser ("Pan"); WeakReference<User> WeakRef =NewWeakreference<user>(user); System.out.println ("WeakRef:" +weakref.get (). GetUserName ()); User=NULL; System.GC (); Try{Thread.Sleep (1000); } Catch(Exception e) {//Todo:handle Exception } //NULLSystem.out.println ("WeakRef:" +weakref.get ()); //-----------test found that the string was not recycled---------//String str = "Hello";//weakreference<string> WR = new weakreference<string> (str);//System.out.println ("WeakRef:" + wr.get ());//str = null;//System.GC ();//System.GC ();//System.GC ();//try {//Thread.Sleep (+);//} catch (Exception e) {// //Todo:handle Exception// }//System.out.println ("WeakRef:" + wr.get ());} @Test Public voidTestsoftref () {User User=NewUser ("Pan"); SoftReference<User> Softref =NewSoftreference<user>(user); System.out.println ("Softref:" +softref.get (). GetUserName ()); User=NULL; System.GC (); Try{Thread.Sleep (1000); } Catch(Exception e) {//Todo:handle Exception } //non-nullSystem.out.println ("Softref:" +softref.get ()); //-----------test found that the string was not recycled---------//String str = "Hello";//weakreference<string> WR = new weakreference<string> (str);//System.out.println ("WeakRef:" + wr.get ());//str = null;//System.GC ();//System.GC ();//System.GC ();//try {//Thread.Sleep (+);//} catch (Exception e) {// //Todo:handle Exception// }//System.out.println ("WeakRef:" + wr.get ()); }}
In Java, Map provides the Weakhashmap class (weakly referenced Class).
Four types of referenced scenarios: primarily when writing Java caching tools or when optimizing memory usage for a program.
Four references and garbage collection strategies in Java