After reading this article, you can basically answer the interview hot spots.

I recently talked about an interview question

Talk about understanding several types of references in Java.

So I’m going to use this interview topic as an example to write an article about what references are in Java and how to use them. 【 Highlight: Give you three seconds to think about how you would respond to this question. 】

What is a quote

References in Java include:

  • FinalReference strong reference
  • SoftReference soft references
  • WeakReference weak references
  • PhantomReference virtual reference

So why are these four citations provided? The main reasons are:

  • Facilitate garbage collection by the Jvm
  • Convenient for developers to use, developers can flexibly decide the life cycle of some objects

How is it used in daily development

So how do we use these quotes in everyday life? Next I will give relevant demo!!

FinalReference strong reference

References such as Object o = new Object() are stored in the JVM stack after an Object is created, and the garbage collector does not reclaim the referenced Object as long as the strong reference exists.

Daily use

Examples of strong references abound, because in everyday development we often try to new an object, and the object that comes out of that new object is strongly referenced, meaning that as long as the reference exists, the garbage collector will not reclaim it. Highlight: How does the JVM know if a reference is present?

SoftReference soft references

Soft reference objects, in the case of insufficient memory, will be included in the garbage collection scope, and then collected, that is, soft reference is not completely safe, in the case of insufficient memory will be collected by the garbage collector.

Given the demo

As you can see from the comments, I instantiated several large objects, placed them in the softReferences array, and then iterated and printed out the names of the objects

As you can see from the results, the first four objects were collected by the garbage collector because of insufficient memory.

Daily use

In the project of our company, soft reference is partly used to store the data taken out from the database. Specifically, an intermediate layer is encapsulated. The function of this intermediate layer is to determine whether the data is NULL when getting out data. This is to avoid memory overflow.

WeakReference weak references

Weak references are weaker than soft references, and objects associated with weak references will only survive until the next garbage collection occurs, that is, when GC occurs, they will be collected regardless of whether there is currently enough memory.

Given the demo

The code is very short, which is to build a weak reference object, print it out before GC to prove that it existed, and then manually call GC to print it out again to show that it is gone. The result is as follows

PhantomReference virtual reference

Virtual references differ from above in that the existence of a virtual reference to an object has no effect on its lifetime and cannot be used to obtain an instance of an object, which means it can be collected by the garbage collector at any time, just as if no reference was associated with it.

So that begs the question, what is the purpose of a virtual reference?

The function is to receive a system notification when the object is collected by the collector, and to track the garbage collector’s collection actions. For example, when the object is collected, the Finalize method of the object will be called.

Before I give you the demo, I want to introduce one

ReferenceQueue ReferenceQueue

ReferenceQueue reference can also be classified as a member of the reference, which can be combined with the above three types of reference [soft reference, weak reference, virtual reference].

So what does it do?

When a Reference is created, the Queue is manually registered to the Reference, and when the objects referenced by the Reference are collected by the garbage collector, the JVM places the Reference on the Queue and we can do other things with the Queue, acting as a notification mechanism.

Given the demo

You can see the usage of queues in the demo

We can see from the result that the first object to get from the reference is null, indicating that an object instance cannot be obtained by virtual reference, and will be queued after being reclaimed.

The concepts associated with Reference

First, to facilitate JVM management, Reference is stateful, which can be divided into the following four states

  • Active The state in which memory is initially allocated. When the reacability of the referenced object changes, gc places the reference in the pending queue and changes its state to pending.
  • Pending refers to objects that are ready to be put into the Pending queue.
  • Enqueue indicates that the object’s memory has been reclaimed.
  • Inactive this is the final state that can’t be changed to any other state.

How does the JVM know if a reference is present

The simple idea of the JVM’s reachability analysis algorithm is to start from a series of GC Roots and search down the path called the reference chain. When an object has no reference chain to the GC Roots, That is, when the object is unreachable from GC Roots, the object is not available and can be recycled. As shown in the figure below

Objects 4, 5, and 6 are all recyclable. Which objects can be used as GC Roots? Let me give you a couple of them, as follows

  • Object referenced in the virtual machine stack

  • The object referenced by the class static property in the method area

  • The object referenced by the constant in the method area

  • Objects referenced by the local method stack JNI

For specific in-depth research, you can search Baidu and Google by yourself, or wait for me to conduct in-depth analysis later.

The last

This article basically answers [talk about the understanding of several references in Java], if you want to further research, it is necessary to start from the source code. The next time you encounter this kind of interview question, you will not be surprised, because in fact, if you read the article carefully and remember a few key points, you will not be asked by the interviewer. In addition, the article also answers the question “how does the JVM know if references are present?” and “which objects can be used as GC Roots?”

Welcome to pay attention to “Fan Talk programming”, share what you have seen, heard, learned and done in the frontline factory!