Sobes.tech
Middle

What types of references exist in Java and what are their features?

sobes.tech AI

Answer from AI

In Java, there are three main types of references (reference variables), which differ in garbage collector behavior:

  • Strong Reference
  • Soft Reference
  • Weak Reference
  • Phantom Reference

Strong Reference

This is the most common type of reference. An object pointed to by a strong reference will not be collected by the garbage collector as long as at least one strong reference to it exists.

// Creating an object and a strong reference to it
Object myObject = new Object(); 

Soft Reference

A soft reference points to an object that can be collected by the garbage collector if the JVM needs memory. Objects only referenced by soft references are guaranteed not to be collected until the JVM urgently needs to free memory. It is commonly used for caching.

import java.lang.ref.SoftReference;

// Creating an object
Object data = new Object(); 
// Creating a soft reference to the object
SoftReference<Object> softRef = new SoftReference<>(data); 

// The data object may be collected if memory is insufficient
// If not collected, it can be retrieved back
Object retrievedData = softRef.get(); 

Weak Reference

A weak reference points to an object that can be collected by the garbage collector at any moment if there are no strong references to it. Weak references are often used for implementing caches where elements can be discarded when memory is low.

import java.lang.ref.WeakReference;

// Creating an object
Object cacheEntry = new Object(); 
// Creating a weak reference to the object
WeakReference<Object> weakRef = new WeakReference<>(cacheEntry); 

// The cacheEntry object can be collected at any moment
// Checking if the object has been collected
if (weakRef.get() == null) { 
    // The object has been collected
}

Phantom Reference

A phantom reference is used in conjunction with a ReferenceQueue to perform actions before the object is fully removed from memory. It is impossible to retrieve the object through a phantom reference (get() always returns null). It is mainly used for resource management that requires special cleanup.

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

// Creating an object for tracking
Object resource = new Object(); 
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>(); 
// Creating a phantom reference and linking it to the queue
PhantomReference<Object> phantomRef = new PhantomReference<>(resource, referenceQueue); 

// In a separate thread, you can monitor the referenceQueue
// When the resource object is ready for final removal, the phantom reference will appear in the queue

Features and comparison:

Reference Type Garbage Collector get() method Usage
Strong Reference No collection Yes Normal usage
Soft Reference May collect Yes (if not collected) Caching (when memory is low)
Weak Reference May collect Yes (if not collected) Caching (if no strong references)
Phantom Reference May collect No (always null) Resource cleanup before removal (with ReferenceQueue)