Unravel โบ Java concepts โบ JVM and performance
Weak and soft references
A normal reference keeps an object alive.
Learn these first
At a glance
A sticky note saying "the report is in drawer 3".
- 1WeakReference<Image> ref = new WeakReference<>(image); ..Image i = ref.get(); returns null once the image was collected, so always check.
- 2WeakHashMap<K, V> removes an entry when its key is no longer strongly referenced elsewhere: useful for metadata attached to objects you do not own
- 3SoftReference suits memory-sensitive caches (kept until the heap is nearly full)Most production caches use Caffeine or Guava with size limits instead.
- 4A ReferenceQueue tells you when the referent was collected so you can clean up the bookkeeping
- Never use a String literal or a boxed small Integer as a WeakHashMap key: they are shared and never collected
- Weak references are non-deterministic; do not use them for correctness
- java.lang.ref.Reference objects are special for the GC: it clears the referent field when the object is only weakly reachable and enqueues the reference
import java.lang.ref.*;import java.util.*;public class Metadata { private final Map<Object, String> labels = new WeakHashMap<>(); void label(Object o, String s) { labels.put(o, s); } private WeakReference<byte[]> cache = new WeakReference<>(null); byte[] data() { byte[] d = cache.get(); if (d == null) { d = new byte[1024]; cache = new WeakReference<>(d); } return d; }}- line 4 uses WeakHashMap, the core of Weak and soft references
- line 6 uses WeakReference, the core of Weak and soft references
- line 9 uses WeakReference, the core of Weak and soft references
Read the full explanation (about 2 min)
A normal reference keeps an object alive. A WeakReference does not: the garbage collector may free the object as soon as no normal reference remains. A SoftReference is freed only when memory is tight. Both are tools for caches.
A mental model
A sticky note saying "the report is in drawer 3". A strong reference is a locked drawer nobody may clear. A weak reference is a note that the cleaning crew is allowed to throw out with the report whenever they pass by.
How it works
WeakReference<Image> ref = new WeakReference<>(image); ... Image i = ref.get(); returns null once the image was collected, so always check.
WeakHashMap<K, V> removes an entry when its key is no longer strongly referenced elsewhere: useful for metadata attached to objects you do not own.
SoftReference suits memory-sensitive caches (kept until the heap is nearly full). Most production caches use Caffeine or Guava with size limits instead.
A ReferenceQueue tells you when the referent was collected so you can clean up the bookkeeping.
Going deeper
Never use a String literal or a boxed small Integer as a WeakHashMap key: they are shared and never collected. Do not let the value refer back to its key, or the entry keeps itself alive.
Weak references are non-deterministic; do not use them for correctness. They are hints about memory, not a replacement for an explicit lifecycle (close()).
Listener registries that hold strong references to listeners are a classic memory leak; weak listeners or explicit removeListener calls fix it.
Under the hood
java.lang.ref.Reference objects are special for the GC: it clears the referent field when the object is only weakly reachable and enqueues the reference. Phantom references are used by Cleaner for post-mortem cleanup.
Common mistakes
- Not checking for null after
get(). - Using weak references as a cache that is expected to hold data.
- Weak keys whose values reference the key.
How Unravel recognises it
When you paste code, Unravel looks for these signs of weak and soft references. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
WeakReferenceSoftReferenceWeakHashMapPhantomReference - Created with new:
WeakReferenceSoftReferenceWeakHashMap
Part of a learning path
- The JVM and the environment ยท previous: Garbage collection ยท next: Memory leaks in Java
Related concepts
Frequently asked questions
What is Weak and soft references?
A normal reference keeps an object alive. A WeakReference does not: the garbage collector may free the object as soon as no normal reference remains. A SoftReference is freed only when memory is tight. Both are tools for caches.
What are the most common mistakes with Weak and soft references?
Not checking for null after get(). Using weak references as a cache that is expected to hold data. Weak keys whose values reference the key.
How can I see Weak and soft references in real code?
Paste your own Java into Unravel and it will point out where weak and soft references appears, explain how it works at the detail level you choose, and draw the flow of the code around it.