Five reference types in Java
In Java, the use of reference types determines the life cycle of the object and the recycling strategy of GC. This article will introduce five reference types in Java and discuss their application in different scenarios.
In Java, reference types can be divided into five types:
- Strong Reference
- Soft Reference
- Weak Reference
- Phantom Reference
- Final Reference
These reference types are mainly used to manage the life cycle of objects, helping the garbage collector to recycle objects that are no longer in use. Next, we introduce each reference type one by one
1. Strong Reference
Strong references are the most common and common reference types in Java. When an object is referenced by a strong reference, the garbage collector will never recycle the object. Even if the heap is insufficient, the program will throw it.OutOfMemoryError
These objects will not be recycled
- Example:
Object obj = new Object(); // Created a strong reference
As shown in the above example, when we passnew Object()
When creating an object, the obj variable holds a strong reference to the object. if onlyobj
The object is still referenced, and the garbage collector will not recycle it.
Features:
- Strongly referenced objects will not be collected by the garbage collector at any time
- Unless the reference is manually set to null
2. Soft Reference
Soft references are an enhancement to strong references that allow recycling of these objects when there is no memory. When the garbage collector finds that memory is insufficient, it recycles all objects pointed to by soft references, thereby avoiding memory overflow. If there is enough memory, these objects can be kept forever
Use soft references to depend onkind:
- Example:
SoftReference<Object> softRef = new SoftReference<>(new Object()); Object obj = ();
Soft references are usually used to implementcachebecause when memory is sufficient, they can retain cached objects and automatically release them when memory is tight
Features:
- Soft reference objects can be retained when memory is sufficient
- Soft reference objects are recycled when memory is tight
3. Weak Reference
Weak references are also a weakened reference type, which is more "frail" than soft references. When the garbage collector runs, regardless of whether the memory is sufficient, as long as the object pointed to by the weak reference is not referenced by other strong references or soft references, it will be recycled.
Use weak references require dependencieskind:
- Example:
WeakReference<Object> weakRef = new WeakReference<>(new Object()); Object obj = ();
Weak references are often used to prevent memory leaks. For example, when designing certain caches or data structures, weak references can ensure that objects are recycled in time when they are no longer strongly referenced.
Features:
- Weak referenced objects will be recycled as long as there is no strong reference during garbage collection.
- Very short life cycle
4. Phantom Reference
Virtual reference is the weakest type of reference, and its existence is mainly used to track the life cycle of an object. When an object is referenced by a virtual reference,get()
Methods always returnnull
. The main function of virtual reference is to receive notifications before the object is garbage collected, so that some cleaning operations can be performed.
Virtual reference needs to be passedclass to implement, and must also be implemented with
ReferenceQueue
Use together:
- Example:
ReferenceQueue<Object> refQueue = new ReferenceQueue<>(); PhantomReference<Object> phantomRef = new PhantomReference<>(new Object(), refQueue);
Virtual references are often used for managementDirect memoryOr perform certain resources release operations, such as file handles, database connections, etc. Recycling of these resources can be triggered by virtual reference
Features:
- Virtual reference cannot directly access objects
- Only used for monitoring objects' garbage collection process
5. Final Reference
FinalReference
is a special reference type that is used to manage objectsfinalize()
method. When the garbage collector finds that an object is not referenced, it will put it in a reference queue and call the object before it is recycledfinalize()
method
Java'sfinalize()
The mechanism allows objects to perform some cleaning work before they are recycled, but this method can easily lead to performance problems and resource leakage, so it is generally not recommended to use it.
- Example:
class MyObject { @Override protected void finalize() throws Throwable { ("Finalize method called"); } } MyObject obj = new MyObject(); obj = null; // Trigger GC and call finalize method
Features:
-
FinalReference
The managed objects will execute the finalize() method before garbage collection. - The last chance to belong to the object
The five reference types in Java provide flexible memory management mechanisms to help developers effectively manage the life cycle of objects according to different scenarios.
Generally speaking, strong references are used for references to ordinary objects, while soft references and weak references can play a role in cache and memory optimization scenarios, while virtual references are mostly used to monitor the recycling of objects.
Understanding and rationally using these reference types is crucial to improving the performance and memory utilization of Java applications.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.