Strong reference

The Reference class and the classes derived from it

When the JVM runs out of memory, it starts garbage collection. For strongly referenced objects, it does not collect them, even if they are in OOM.

Object o1 = new Object();Copy the code

Strong references are our most common plain object references, and as long as there is a strong reference to an object, it indicates that the object is “alive” and won’t be touched by the garbage collector. The most common in Java is strong references, where an object is assigned to a reference variable that is a strong reference. When an object is referenced by a strongly referenced variable, it is in a reachable state and cannot be collected by the garbage collection mechanism, even if the object will never be used or collected by the JVM. So strong references are one of the main causes of Java memory leaks.

An ordinary object that has no other reference relationship is generally considered garbage collectible if it exceeds the scope of the reference or explicitly sets the corresponding (strong) reference to NULL (depending on the garbage collection policy).

Soft reference SoftReference

Java. Lang. Ref. SoftReference class implementation, you can keep objects out of garbage collection.

Features:

1. When the system is full of memory, it will not be reclaimed. 2. When the system runs out of memory, it is reclaimed.Copy the code

Application Scenario Example

The cache is useful for soft references, which are retained when enough memory is available and reclaimed when insufficient.Copy the code
Public class SoftReferenceDemo {/** * when memory is sufficient * -xx :+PrintGCDetails */ public static void softRefMemoryEnough() {// Create a strong application Object o1 = new Object(); // create a SoftReference SoftReference<Object> SoftReference = new SoftReference<>(o1); System.out.println(o1); System.out.println(softReference.get()); o1 = null; // manually GC system.gc (); System.out.println(o1); System.out.println(softReference.get()); } /** * JVM configuration, intentionally generate large objects and configure small memory, so that it runs out of memory causes OOM, -xms2m -xmx2m -xx :+PrintGCDetails */ public static void softRefMemoryNoEnough() { System.out.println("========================"); O1 = new Object(); // create a SoftReference SoftReference<Object> SoftReference = new SoftReference<>(o1); System.out.println(o1); System.out.println(softReference.get()); o1 = null; Byte [] bytes = new byte[10 * 1024 * 1024]; } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(o1); System.out.println(softReference.get()); } } public static void main(String[] args) { softRefMemoryEnough(); softRefMemoryNoEnough(); }}Copy the code

## WeakReference

Whenever the JVM does garbage collection, the memory occupied by the object is reclaimed.

public class WeakReferenceDemo { public static void main(String[] args) { Object o1 = new Object(); WeakReference<Object> weakReference = new WeakReference<>(o1); System.out.println(o1); System.out.println(weakReference.get()); o1 = null; System.gc(); System.out.println(o1); System.out.println(weakReference.get()); }}Copy the code