Checks whether the object is dead

To judge whether objects are dead is to find out which objects are dead and will not be used in the future, just like waste paper, drink bottles and hundred-dollar bills on the ground. Before sweeping the floor, we should judge that waste paper and drink bottles on the ground are garbage, and hundred-dollar bills are not garbage. There are reference counting algorithms and reachability analysis algorithms to determine whether an object is dead.

1. Reference counting algorithm

Add a reference counter to each object, incrementing the counter each time a reference is made to it; Whenever a place no longer refers to it, the counter value is reduced by 1, so that as long as the value of the counter is not zero, it is referred to somewhere else and it is not a useless object. In the figure below, object 2 has one reference and its reference counter value is 1. Object 1 has two local references and its reference counter value is 2.

This method looks very simple, but now many of the mainstream of the virtual machine is no use this algorithm to manage memory, the reason is that when some object reference each other between, unable to determine whether the object is dead, as the chart, the object 1 and object 2 have never been outside the heap variable references, but by the other party reference each other, though they didn’t use at this time, However, the reference counter still has a value of 1, so there is no way to determine that they are dead objects and the garbage collector cannot collect them.

2. Accessibility analysis algorithm

Before getting to know the accessibility analysis algorithm, let’s understand the concept of GC Roots, the starting point for garbage collection, The objects that can be used as GC Roots are the objects referenced in the local variable table in the virtual machine stack, the objects referenced by static attributes in the method area, the objects referenced by constants in the method area, and the objects referenced by JNI (Native methods) in the local method stack.

When an object is not connected to GC Roots by any reference chain (GC Roots are unreachable to this object), the object is unavailable and dead.

The diagram below: There are reachable paths between OBJECT1, Object2, Object3, Object4 and GC Roots. These objects will not be recycled, but there are no reachable paths between OBJECT5, Object6, object7 and GC Roots. These objects are condemned to death.


Object5, Object6, Object7 are not doomed to death. There is room for redemption. When there is no reference chain between objects and GC Roots after the reachabability analysis, objects will be marked once, and then objects will be executed (cleared) if finalize() method does not overwrite objects or has been called by virtual machine. If the object overwrites the Finalize () method and has not been called yet, the content of finalize() method will be executed, so you can save yourself by re-associating with the object in the GC Roots reference chain, but it is generally not recommended to do so. Zhou zhiming also suggested that you should forget this method completely

3. Method area recycling

All of the above is the judgment of objects in the heap memory, the method area is mainly recycled discarded constants and useless classes.

You can determine if there is a place to reference the constant. If there is no reference, the constant is discarded.

To determine whether a class is discarded, the following conditions must be met:

All instances of the class have been reclaimed (there are no instances of the class in the heap).

The ClassLoader that loaded the class has been reclaimed.

The java.lang.Class object corresponding to this Class is not referenced anywhere (there are no methods to access this Class through reflection).

Four garbage collection algorithms are commonly used

There are four commonly used garbage collection algorithms: mark-sweep algorithm, copy algorithm, mark-collation algorithm, generational collection algorithm.

1. Mark-clear algorithm

There are two stages, marking and clearing. Firstly, all objects to be recycled are marked, and all marked objects are recycled after marking, as shown in the following figure.

Disadvantages: low efficiency in both marking and cleaning processes; A large number of discrete memory fragments are generated after the flag is cleared.

2. Replication algorithm

The memory is divided into two pieces of equal size, and only one piece is stored each time. When this piece is used up, all surviving objects are copied to the other piece, and the used memory space is cleared, and the cycle repeats, as shown in the following figure.

Disadvantages: the actual available memory space is reduced to half of the original, more suitable.

3. Mark-tidy algorithm

The available objects are marked, then all marked objects are moved to a segment, and finally the memory beyond the boundaries of the available objects is cleared, as shown in the figure below.

4. Generational collection algorithm

The heap memory is divided into new generation and old generation, and the new generation is divided into Eden region, From Survivor and To Survivor. In general, objects in the new generation are basically born and die, and only a small number of objects survive each time. Therefore, using the replication algorithm, only those small number of surviving objects can be copied to complete garbage collection. In the old age, the survival rate of the object is higher, so the algorithm of mark-clearing and mark-sorting is used for recycling.

Garbage collection in these areas may be as follows:

In most cases, new objects are allocated in Eden. When there is no space in Eden for allocation, a Minor GC is performed to clean up the useless objects in Eden. After clearing, the surviving objects in Eden and From Survivor enter To Survivor if the available space is smaller than that of To Survivor, otherwise enter the old age directly); The age of the Eden and From Survivor objects that are still alive and can enter To Survivor is increased by one year (the virtual machine defines an age counter for each object, incrementing the age of each Minor GC), When the age of the living object reaches a certain level (15 by default), the age can be set by -xx :MaxTenuringThreshold.

After Minor GC, Eden will not be able to allocate space for the new object (which must be large), and the new object will go straight to the old age.

For objects that occupy more than half of the To Survivor space and are of the same age, those whose age is greater than or equal To this age will directly enter the old age. For example, if the Survivor space is 10M, several objects whose age is 4 occupy a total space of more than 5M, then all objects whose age is greater than or equal To 4 will directly enter the old age. There is no need to wait until MaxTenuringThreshold specifies an age.

Before the Minor GC, will determine whether the old s largest continuous space available is greater than the new generation total space, all objects if greater than, that Minor GC is safe, otherwise it will determine whether to allow guarantees failure, if allowed, whether old s largest continuous space available is greater than all previous promotion to the old s average size of the object, If greater, Minor GC is performed, otherwise Full GC is performed.

When calling System.gc() directly from Java code, the JVM is recommended to perform Full GC, but Full GC is usually triggered and is not recommended. Try to let the VIRTUAL machine manage gc policies.

The permanent generation (method area) is used to store class information. In JDK1.6 and earlier, the permanent generation also stores constants, static variables, etc. When the space of the permanent generation is insufficient, the Full GC is also triggered. If the permanent generation cannot store new data through the Full GC, the permanent generation will throw an out-of-memory exception.

Large objects (objects that require a lot of contiguous memory), such as long arrays, go straight to the old age, and if there is not enough contiguous space for the old age, Full GC is performed.

The JVM series:

In-depth analysis of JVM memory regions and memory overflow analysis

JVM to determine whether an object is dead and four garbage collection algorithms

The JVM configures common parameters and common GC tuning policies

7 JVM garbage collector features, Pros and cons, and usage scenarios!

The last

The follow-up will continue to update the performance optimization knowledge, write bad places also hope Daniel can give advice, we feel good can point a thumbs-up in the attention to me, just entered, will share more articles in the future!

Share free learning materials
We will also prepare free Java architecture learning materials (including high availability, high concurrency, high performance and distribution, Jvm performance tuning, MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, Nginx multiple knowledge architecture material etc.)
The reason why someone is always better than you is because they are already better and are constantly trying to become better, while you are still satisfied with the status quo and secretly happy!

Pay attention to the background private letter keywords [structure] can be free access to information, no routine concerns about the public number for free, and the public will continue to update the technology dry goods and fans welfare!

(Some information below)