Java difference between strong/soft/weak/phantom reference
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Java is a widely used, object-oriented programming language that provides a robust framework for building a wide range of applications. Among its rich set of features, Java offers different types of object references: strong, soft, weak, and phantom references. These references are an integral part of Java's memory management and garbage collection mechanism and play a critical role in controlling the lifecycle of objects. Understanding these references can help developers manage memory more efficiently, prevent memory leaks, and optimize application performance. This article explores the differences between these reference types and provides technical explanations and examples.
Strong References
Explanation
In Java, a strong reference is the most common type and the default form of reference. When an object is reachable through a strong reference chain, it is considered "strongly reachable." As long as a strong reference to an object exists, the garbage collector will not reclaim that object.
Example
In this example, the strongReference variable holds a strong reference to the String object containing "Hello, World!". The garbage collector will not attempt to reclaim this String object as long as the strong reference exists.
Soft References
Explanation
A soft reference is one level weaker than a strong reference. Objects referenced only by soft references are eligible for garbage collection, but they will generally be reclaimed only when the JVM is low on memory. Soft references are useful for implementing memory-sensitive caches.
Example
To retrieve the object from a SoftReference, you can use the get() method:
Weak References
Explanation
A weak reference is weaker than a soft reference. Objects that are only weakly reachable are quickly reclaimed by the garbage collector when they are no longer in use. The primary use case for weak references is canonicalized mappings such as WeakHashMap.
Example
To access the object held by a WeakReference, you also use the get() method:
Phantom References
Explanation
Phantom references are the weakest type of reference. They are used to determine exactly when an object has been removed from memory. Unlike soft and weak references, you cannot retrieve an object from a phantom reference's get() method. Phantom references are primarily used in conjunction with ReferenceQueue for performing post-mortem cleanup actions.
Example
To determine when an object becomes phantom reachable:
Table Summary
Here's a summary of key points for each type of reference in Java:
| Reference Type | Description | Collected at | Can Access Object? |
| Strong Reference | Default reference type in Java | Never, unless no longer referenced | Yes |
| Soft Reference | Suitable for caches | When memory is low | Yes, via get() |
| Weak Reference | For canonicalized mappings | Once no strong references exist | Yes, via get() |
| Phantom Reference | Used for cleanup after GC | Determined but not collected until dereferenced | No |
Additional Considerations
- Reference Queues: Soft, weak, and phantom references can be associated with a
ReferenceQueue. When the garbage collector discovers that the referent has become softly, weakly, or phantom reachable, it adds the reference object to the associated queue. - Memory Management: By choosing the appropriate type of reference, developers can manage memory consumption effectively, potentially improving application performance.
- Java Memory Model: Understanding how different references work can aid in understanding Java's memory model and the behavior of the garbage collector.
By effectively utilizing the different types of references in Java, developers can manage object lifecycles more efficiently, helping maintain an application's performance and resource usage.
Related reading
- Java G1 Old generation garbage collection count is 0
- java get file size efficiently
- Java health monitoring in clustered environment
- Java heap terminology young, old and permanent generations?
- Java Does wait release lock from synchronized block
- Java EE EJB 3.0 Glassfish
- Java in RETURN statements?
- Java JVM profiling, thread status - what does Monitor status mean?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.