This is the 26th day of my participation in Gwen Challenge
Garbage collection references
An overview of the
After JDK1.2, Java expanded the concept of references into strong references, soft references, weak references, and virtual references, and the strength of these four references gradually decreased.
- StrongReference: the most common reference method, traditional
new
Object is a strong reference. In any case, the garbage collector will never reclaim the referenced object as long as the strongly referenced relationship exists.Never recycle - SoftReference: objects are reclaimed for a second time before a memory overflow occurs. OOM will appear if there is still not enough memory. Insufficient memory is reclaimed
- WeakReference: objects associated with weak references only survive until the next garbage collection. Whenever the garbage collector is triggered, objects associated with weak references are collected regardless of whether there is enough memory. Find and recycle
- Virtual reference: Whether an object has a virtual reference has no impact on its life cycle. A virtual reference cannot be used to obtain an instance of an object. The sole purpose of setting a virtual reference to an object is to receive a system notification when the object is collected by the garbage collector. Object recovery trace
All of the above cases are in the case of reachable objects.
Strong reference
One of the most commonly used references in Java is also the default reference type. Strongly referenced objects are touchable and can never be collected by the garbage collector. An ordinary object that has no other reference relationship can be garbage collected as long as it exceeds the scope of the reference or explicitly assigns the reference to NULL.
In contrast, soft-referenced, weak-referenced, and vy-referenced objects are soft-touchable, weak-touchable, and vy-touchable objects, which are deplorable under certain conditions. Therefore, strong references are one of the main causes of Java memory leaks.
Strong reference features:
- Strong references allow direct access to the target object.
- Strong references to objects are never recycled, resulting in OOM not being recycled either.
- Strong references can lead to memory leaks.
Soft references
Soft references describe objects that are useful but not necessary. Objects associated with soft references will be recycled in the recycle range for a second time before they run out of memory. If they run out of memory, OOM will be thrown. This overflow has nothing to do with soft references.
When constructing a soft reference, you can specify a reference queue. When a soft reference object is reclaimed, it will be added to the specified queue. This object can be used to track object reclamation.
Soft references are often used to implement memory-sensitive caches, such as caches, where if there is free memory, the cache can be retained temporarily and cleaned up when it runs out of memory.
The JVM tries to keep soft references alive as long as possible and cleans them up when necessary.
Soft reference implementation is as follows:
SoftReference<Object> sf = new SoftReference<>(new Object());
Copy the code
A weak reference
Weak references, like soft references, describe objects that are not required. Objects that are only associated with weak references will only survive until the next garbage collection. During GC, as long as there are weak references, objects that are only associated with weak references will be collected regardless of whether there is enough space.
However, because the thread priority of garbage collection is low, objects associated with weak references may live longer.
As with soft references, you can use reference queues to hold objects associated with weak references.
Weak references are implemented as follows:
WeakeReference<Object> wf = new WeakeReference<>(new Object());
Copy the code
The biggest difference between soft references and weak references is that soft references are out of memory and recycled, while weak references are discovered and recycled. Weak references are easier and faster to be reclaimed by GC than soft references.
Phantom reference
Virtual references are the weakest of all reference types, and the existence of virtual references does not determine the life cycle of an object at all. If an object has only virtual references, it is almost as likely to be collected by the garbage collector at any time as if it had no references.
You cannot use it alone, nor can you get the referenced object through virtual reference fetching.
The sole purpose of a virtual reference is to track the garbage collection process, such as receiving a system notification when an object is collected.
A virtual reference must be used with a reference queue, and a reference queue is provided as an argument when it is created. When the garbage collector is about to reclaim an object and finds that it has a virtual reference, it adds the virtual reference to the reference object after the object is reclaimed to inform the application of the object’s recycling status.
Because a virtual reference can track the reclaim time of an object, some resource release operations can also be performed and logged in a virtual reference.
Virtual references are implemented as follows:
ReferenceQueue q = new ReferenceQueue();
PhantomReference <Object> pf = new PhantomReference<>(new Object(), q);
Copy the code