This is the 21st day of my participation in the August More Text Challenge

This paper continues the previous article | the JVM garbage collection strategy (2), continue to speak in the JVM reference type classification.

3.3 Classification of Reference Types

We know that Java can create an object using new, as shown in the figure below.

final Object obj = new Object();
Copy the code

So what type of reference does the variable obj refer to the object being created? The answer is strong references.

Strong Reference, as shown in the code above, is never recycled as long as the Strong Reference is present. These objects will not be reclaimed even if there is an overflow of memory. The way objects are referenced in this way, they won’t be GC unless they don’t have any path linked to the root object.

Are strongly referenced objects manually reclaimed?

# obj = null;Copy the code

So the Java language provides soft references, which describe objects that are useful but not necessary. Instead of strong references, software references are associated with objects that are reclaimed by the JVM only when memory is low.

SoftReference<String> ref = new SofteReferencec<>("value");
Copy the code

So where can soft references be used? As we discussed in an earlier article, soft references can be used to implement a simple memory-based cache implementation.

AtomicReference<SoftReference<List<<String>>> = new AAtomicReference();
Copy the code

SoftReference temporarily stores some useful but unnecessary objects. If the memory is insufficient, SoftReference can be reclaimed. AtomicReference ensures read and write security in concurrent environments. For example, it can be used

  • Interface Mock
  • Web caching
  • Image caching.

Soft references are typically recycled when out of memory. Are referenced objects recycled whenever GC occurs?

Of course, the Java language provides Weak references. Weak references are objects that can be harvested and recycled as soon as the JVM garbage collector finds them. Weak references are used to describe non-essential objects and are created as follows.

WeakReference<String> ref = new WeakRefence<>("value");
Copy the code

The scenario for weak references is for memory-sensitive caches. For example, java.util.WeakHashMap keys use weak references.

Speaking of this, we need to introduce an important ReferenceQueue ReferenceQueue. When we use SoftReference and WeakReference, in addition to the usage mentioned above, we also have another way of use.

WeakReference(T referent, ReferenceQueue<? super T> q)
Copy the code

That is, passing in a ReferenceQueue that is added to the associated ReferenceQueue when the garbage collector is ready to reclaim a referenced object. A program can determine whether a referenced object is collected by GC by determining whether a reference has been added to the reference queue.

For SoftReference and WeakReference, we can only check whether the object is recovered in the reference queue after the object is recovered. Is there a way to receive a notice before the object is recovered to do some corresponding processing?

Of course, the Java language provides Phantom references, sometimes referred to as ghost references. Virtual references do not affect the life cycle of an object. If an object has only virtual references, it can be collected by the garbage collector at any time, just as if it had no references at all. The creation method is as follows.

ReferenceQueue<String> queue = new ReferenceQueue<>();
PhantomReference<String> ref = new PhantomReference<>("value",queue);
Copy the code

So what’s the use of it? Virtual references are used to track the activity of an object being collected by the garbage collector. To put it bluntly, they observe whether the object is being collected by the garbage collector, and if it is, they receive a system notification.

In contrast to other references, virtual references must be used in conjunction with reference queues, and virtual references cannot be used to obtain a real reference to an object.

When the garbage collector to recycle an object, if you find it and virtual reference, will be before the recovery of the object’s memory, add the phantom references to associated with reference queue, program can be referenced by judging whether the queue have joined the virtual reference, to see if the referenced object will be reclaimed by the garbage collector. If the program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is reclaimed.

For example, the following method is used to determine whether a virtual reference has been added to the reference queue.

while (true) { Reference poll = queue.poll(); // reference queue if (poll! = null) {system.out.println (" virtual reference was reclaimed: "+ poll); }}Copy the code

Didn’t see enough, please jump to the next article JVM | garbage collection strategy (3)