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“
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]
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]:
- Strong references may be insensitive to garbage collection activities
- Self inheriting reference to implement custom reference types is of little significance.
- Final reference, which is a specialized virtual reference
- 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
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.
[ ⚠️ Precautions]:
- 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();.
- Soft reference, weak reference and virtual reference are all direct subclasses of java.lang.ref.reference.
- 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.
- 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.