1. StrongReference

A strong reference is the most common way to define a variable, namely:

    // These are the most common ways to define variables
    val strongReference="Strong reference"
    val strongReferenceList= mutableListOf<String>("Strong"."Lead"."With")
Copy the code
  • Strong references allow direct access to the target object.
  • Strong references point to objects that are not reclaimed by the system at any time. When running out of memory, the Java virtual machine would rather throw an OutOfMemoryError than reclaim the object to which the strong reference points.
  • Strong references can lead to memory leaks.

2. SoftReference

A SoftReference is one that defines an object and then adds the object to a SoftReference. You can call the get() function in SoftReference to obtain an object.

    // load a String into a softReference softReference
    val softReference=SoftReference("Soft reference")
    // Use the get() function to fetch a String
    softReference.get(a)Copy the code

If the object to which the soft reference points is recycled, the get() function will return null. Therefore, you need to check whether the object is null to avoid a NullPointerException that may cause program crash.

    • If an object has only soft references, then if there is enough memory, the object referred to by the soft reference will not be reclaimed, and the call to get() will return the currently pointed object; When memory runs out, it is reclaimed, and a call to get() returns null.
  • Soft references can be used to implement memory sensitive caching.
  • A soft reference can be associated with a ReferenceQueue (ReferenceQueue), and if the object it points to is reclaimed, the Java virtual machine adds the soft reference to its associated ReferenceQueue, ReferenceQueue.
ReferenceQueue (ReferenceQueue)
    The referenceQueue type must be the same as that of softReference
    val referenceQueue: ReferenceQueue<String> = ReferenceQueue()
    // Pass a reference queue for SoftReference
    val softReference=SoftReference("Soft reference",referenceQueue)
    
Copy the code
  • When the object to which the soft reference refers is reclaimed, the soft reference will be passed into the referenceQueue referenceQueue (which stores all reclaimed references).
  • When we call the poll() method that refers to a queue, the Reference object in front of the queue is returned if the queue is not empty. If the queue is empty, a null value is returned.

The reference queue is used to check whether the object to which the reference refers has been reclaimed. And do some simple operations.

3. WeakReference

  • If an object has only weak references, when the garbage collector thread finds an object with only weak references during its scanning, it will immediately reclaim the object regardless of whether there is sufficient memory.
  • However, since the garbage collector thread priority is very low, the object with weak references will not be scanned immediately and there will be a delay.
    // Create a weak reference object that points to a String weak reference.
    val weakReference=WeakReference("Weak reference")
    // Call get() to get the String to point to
    weakReference.get(a)Copy the code

4. Virtual Reference

  • Virtual references are mostly used to track the activity of an object being collected by the garbage collector.
  • Virtual references differ from other references in that they must be used in conjunction with the ReferenceQueue of the ReferenceQueue
  • When the garbage collector finds an object with only virtual references, it reclaims the object and adds its virtual references to the reference queue
    // Reference queue
    val phantomQueue = ReferenceQueue<String>()
    // Virtual references must be used with phantomQueue reference queues
    val phantomReference=PhantomReference("Virtual reference", phantomQueue)
Copy the code

conclusion

  • Priority: Strong reference (not reclaimed) > Soft reference (reclaimed when out of memory) > Weak reference (reclaimed at any time) > Virtual reference (reclaimed at any time, rarely used)
  • When a program has a memory leak or overflow, you can consider using soft or virtual references.
  • A virtual reference is just an activity that checks for objects being reclaimed. Debug?
  • If an object has both strong references and other references, the object with the highest priority will not be reclaimed. The same goes for other cases.

In Android, Activity A passes the current Activity A Context to another component B, and component B gets A reference to Activity A. When Activity A is destroyed and component B does not need to be destroyed, Activity A will not be destroyed in time because component B has A strong reference to Activity A’s context, causing A memory leak.