preface
Today, let's learn some basic concepts related to JVM garbage collection mechanism.
How to judge whether an object is alive
The primary task of the garbage collector is to determine which objects are alive and which objects have died (dead here means that the object is no longer used by any way).
Reference counting algorithm
The reference counting algorithm is to add a reference counter to the object. Whenever a place references it, the counter value will be increased by one; When the reference fails, the counter value decreases by one; An object whose counter is zero at any time is an object that cannot be used.
The disadvantage of reference counting algorithm is that it is difficult to solve the problem of circular reference between objects.
Reachability analysis algorithm
Using a series of root objects called "GC Roots" as the starting node set, start from these nodes and search downward according to the reference relationship. The path taken by the search process is called "reference chain". If there is no reference chain between an object and GC Roots, or in the words of graph theory, when it is inaccessible from GC Roots to this object, it proves that this object can no longer be used. For example, through Objcet 5, Object 6 and Object 7, three objects are already connected, but they are inaccessible to GC Roots, so they can be determined as recyclable objects.
Fixed objects that can be used as GC Roots include the following:
Objects referenced in the virtual machine stack (local variable table in the stack frame), such as parameters used in the method stack called by each thread, local variables, temporary variables, etc Objects referenced by class static attributes in the method area, such as Java Reference type static variable of class Objects referenced by constants in the method area, such as references in the string constant pool 4.In the local method stack JNI(That's what we usually say Native method)Referenced object Java The basic data type of the virtual machine, such as the internal reference Class Object, some resident exception objects (such as NullPointException,OutOfMemoryError)And so on, as well as the system class loader. All synchronized locks( synchronized Keywords) held objects 7.reflect Java Internal conditions of virtual machine JMXBean,JVMTI Callback registered in, local code cache, etc
Classification of references
There are four kinds of references:
Strong reference: strong reference refers to the reference assignment commonly existing in program code, that is, similar“ Object obj=new Object()"This reference relationship. In any case, as long as the strong reference relationship still exists, the garbage collector will never recycle the referenced object Soft reference: soft reference is used to describe some useful but unnecessary objects. Objects that are only associated with soft references will be listed in the recycling scope for the second recycling by these objects before the memory overflow exception will occur in the system. If there is not enough memory in this recycling, the memory overflow exception will be thrown. Weak reference: weak reference is also used to describe those unnecessary objects, but its strength is weaker than soft reference. The objects associated with weak reference can only survive until the next garbage collection occurs. When the garbage collector starts working, objects associated with only weak references will be recycled regardless of whether the current memory is sufficient or not. Virtual reference: whether an object has a virtual reference will not affect its lifetime at all, nor can it be used to obtain an object instance through virtual reference. The only purpose of setting a virtual reference Association for an object is to receive a system notification when the object is recycled by the collector.
Recovery method area
Garbage collection in the method area mainly recycles two parts: discarded constants and types that are no longer used. Recycle waste constant
It is very similar to recycling objects in Java heap. At the same time, it is relatively simple to judge whether a constant is discarded, and the conditions to judge whether a type belongs to a "class that is no longer used" are more stringent. The following three conditions need to be met at the same time.
All instances of this class have been recycled, that is Java There are no instances of this class and any derived subclasses in the heap. The class loader that loads this class has been recycled, which is usually difficult to achieve Corresponding to this class java.lang.Class Object is not referenced anywhere, and methods of this class cannot be accessed anywhere through reflection.
Generational collection theory
Generational collection theory is essentially a set of empirical rules in line with the actual situation of most programs. It is based on two generational hypotheses.
Weak generation hypothesis: most objects are born in the morning and die in the evening. Strong generational hypothesis: the more times an object survives the garbage collection process, the harder it is to die The collector should Java The heap is divided into different areas, and then the recycled objects are allocated to different areas for storage according to their age (age is the number of times the object has survived the garbage collection process). Obviously, if most objects in an area are born and die day by day and it is difficult to survive the garbage collection process, put them together and focus on how to keep a small number of objects alive each time, rather than marking a large number of objects to be recycled. You can reclaim a lot of space at a lower cost;If the remaining objects are hard to die, put them together, and the virtual machine can reclaim the area with a lower frequency. Cenozoic: a large number of objects are found dead during each garbage collection, while a small number of objects survive after each collection, Will be gradually promoted to the middle of the old age. Old age: almost all objects in old age are Survivor If they survive in the area, they won't "die" so easily. Cross generational citation hypothesis: cross generational citation is only a small number compared with the same generation citation It is only necessary to establish a global data structure (this structure is called "memory set") on the new generation. This structure divides the old generation into several small blocks to identify which memory in the old age will have cross generation references. After that, when it happens Minor GC Only small memory objects that contain cross generational references will be added to the GC Roots Scan.
. . . . . . . . . . . . . . . . .
For copyright reasons, please refer to the following for the complete article: JVM 03 -- some basic concepts of JVM garbage collection mechanism