Analysis of java reference mechanism in Java programming development

Time:2021-9-10

For a Java object, there are two main types of storage: one is the memory heap, which is unordered and is mainly used to store the created Java object; One is the memory stack, which is mainly used to store Java references, and then use Java references to point to Java objects in the management process.

About “those things” referenced by reference“

BW1nxS.jpg

With the golden merry floating on the sea, everything looks so calm, but everyone seems to be busy with what they are doing… Suddenly Webb asked Saab a question: we all know that JVM tuning is an important part of java development. You know heap and stack, What is the respective storage mechanism? For this question, just like the dream of exploring the end of the world, Saab answered Weber:For a Java object, there are two main types of storage: one is the memory heap, which is unordered and is mainly used to store the created Java object; One is the memory stack, which is mainly used to store Java references, and then use Java references to point to Java objects in the management process.After listening to Saab’s answer, Webb leaned on the deck, lit a scarlet cigarette, took a sip, and then said to Saab: can you explain the citation mechanism in detail? Saab thought for a while, so he had the following story

Basic principle of GC [garbage collectors]

s58VjP.png

Before introducing the concept of reference, we need to know the structure distribution and basic situation of memory stack and heap in JVM virtual machine, as shown in the figure above. To sum up, we can understand that JAVA memory management is actually object management, including object instance allocation and release. The existence of GC is responsible for recycling objects when they are “unreachable”. When the system creates an object instance, that is, when it uses the new keyword to create an object, GC starts to monitor the address, size and usage status of the object. Generally, Java’s GC mechanism has a specific recycling algorithm. GC usually records all objects in the queue in the way of directed graph, and determines or even marks which objects are “reachable” and which are “unreachable”. When GC judges that some objects are unreachable, GC is responsible for recycling the relevant memory space and whether it can be recycled by the garbage collection mechanism. The specific operation depends on the machine and platform, but the judgment is mainly based on whether the object has a reference to the object.

Reference basic overview

Abstract base class for reference objects. This class defines the operations common to all reference objects. Because reference objects are implemented in close cooperation with the garbage collector, this class may not be subclassed directly.

It is not difficult to find that the reference mechanism is sensitive to the garbage collection activities of the JVM. The inheritance relationship or implementation of the reference is customized by the JDK, and the reference instance is created by the JVM. It is generally not recommended to inherit the reference to implement the customized reference type, but the existing reference type can be inherited.
JDK provides strong reference, soft reference, weak reference, phantom reference, ReferenceQueue, final reference and other reference types.

[ ⚠️ Precautions]:

  1. Strong references may be insensitive to garbage collection activities
  2. Self inheriting reference to implement custom reference types is of little significance.
  3. Final reference, which is a specialized virtual reference
  4. For different JDK versions, you need to pay attention to the comparative analysis of the actual source code.
    5. Reference is the base class of all reference objects

Reference reference type analysis

s4EtN8.png

Strong reference: in Java, the most common is strong reference, which is also the most common reference type. If the object is reachable, it cannot be recycled by the garbage collection mechanism. Even if the object will never be used in the future, the JVM will not recycle it.

Soft reference: for objects with only soft references, it will not be recycled when the system memory is enough, and it will be recycled when the system memory space is insufficient. Soft references are often used in memory sensitive programs.

Weak reference: the lifetime is shorter than that of soft references. For objects with only weak references, as long as the garbage collection mechanism runs, the memory occupied by the object will always be recycled regardless of whether the memory space of the JVM is sufficient.
Phantom reference: cannot be used alone. It must be used in combination with reference queue. The main function of virtual reference is to track the state of the object being garbage collected.
ReferenceQueue
Final reference

[ ⚠️ Precautions]:

  1. Strong references may be insensitive to garbage collection activities and have no corresponding type representation, that is, strong references are common, such as object = new object();.
  2. Soft reference, weak reference and virtual reference are all direct subclasses of java.lang.ref.reference.
  3. Up to jdk11, there are only four kinds of references. These references are created by the JVM. Therefore, it is invalid to directly inherit java.lang.ref.reference to create custom reference types, but you can directly inherit existing reference types. For example, java.lang.ref.cleaner inherits from java.lang.ref.phantom reference.
  4. The subclass java.lang.ref.finalreference of the special java.lang.ref.reference is related to object #finalize(). Java.lang.ref.finalize is a subclass of java.lang.ref.finalreference.
    Copyright notice: This article is the original article of the blogger and follows the relevant copyright agreement. If you reprint or share, please attach the source link and link source of the original text.

Recommended Today

Some useful online tools (2)

Some useful online tools (2) Online tools This article continues with a similar article on some useful tools 1. Online Python (1)https://code.y444.cn/python (2)https://tool.lu/coderunner/ 2. Quantum computing related websites (1)https://zh.wikipedia.org/wiki/%E9%87%8F%E5%AD%90%E8%A8%88%E7%AE%97%E6%9A%A8%E9%87%8F%E5%AD%90%E4%BF%A1%E6%81%AF (2)https://s4plus.ustc.edu.cn/2018/1225/c17737a372334/page.htm 3. Some entertainment websites (1)https://www.ratoo.net/ (2)https://www.wangtaier.cn/post/152559.html (3)https://www.wangtaier.cn/ 4. Xiaojiaoya development website https://www.stepfpga.com/ The above are some useful online websites. I hope they can help you. […]