1. Sorting out garbage recycling algorithm
How do I calculate that an object is dead
1.1 Reference Counter Algorithm The reference counter algorithm sets a counter for each object. When the object is referenced somewhere, the counter is +1, when the reference is invalid, the counter is -1, and when the counter is 0, the JVM considers the object to be no longer in use and “garbage.”
Reference counter is simple to implement and high efficiency. It does not solve the problem of circular reference questions (object A refers to object B, and object B refers to object A, but A and B are not referenced by any other objects), and there is A lot of extra overhead with each increment and decrement of counters, so this algorithm has been discontinued after JDK1.1.
1.2 Reachability Analysis Algorithm The reachability analysis algorithm starts from some “GC Roots” objects and searches down from these nodes. The search path becomes the Reference Chain. When an object is not connected by the Reference Chain of GC Roots, Indicates that the object is not available.
GC Roots objects include:
Object referenced in the virtual machine stack (the local variable table in the stack frame).
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 JNI (Native method) in the Native method stack.
In Java, objects will be marked first and then protected void Finalize () will be called. In this case, objects will be saved. As long as the method connects the object to the reference chain, it can escape being recycled
This is easy to understand. In general, permanent zones are the most difficult to recycle. Permanent generation mainly recycles the following parts:
Discard classes with constants that are useless
Garbage collection algorithm
3.1 Mark – Clear Algorithm The mark – clear algorithm consists of two stages: “mark” and “clear”. In the marking phase, all objects to be reclaimed are identified and marked. The clear phase follows the mark phase, purging objects that the mark phase determined were not available.
The mark-sweep algorithm is a basic collection algorithm. The efficiency of the mark and sweep phase is not high, and the large amount of discontinuous space will be generated after the sweep, so when the program needs to allocate large memory objects, it may not be able to find enough continuous space.
3.2 Replication Algorithm The replication algorithm divides the memory into two equal pieces, uses one piece each time, and when garbage collection is made, copies the surviving objects to the other piece, and then cleans up the entire memory. This sounds like a great solution, but overall it is very memory intensive.
The replication algorithm is simple to implement and runs efficiently. However, only half of the replication algorithm can be used at a time, resulting in low memory utilization. Today’s JVMS use replication to collect the new generation, and since most objects in the new generation (98%) are ephemeral, the ratio of two pieces of memory is not 1:1(about 8:1), the oft-mentioned Eden(80%) and Survivor(20%). Of course, there will also be a situation where 10% is not enough, which will be sorted out later, and there will be a compensation mechanism, that is, allocation guarantee.
3.3 Mark-collation algorithm
One extreme of the copy-collection algorithm is that none of the objects are dead. This kind of situation will have the probability to appear in the old age, so according to the characteristics of the old age put forward the mark-collation algorithm. The mark-declaim algorithm is the same as the mark-clean algorithm, but instead of copying the live object to another block of memory, the mark-declaim algorithm moves the live object to one end of memory, and then directly reclaiming the memory beyond the boundary, as shown in the following figure:
3.4 Generational Collection In generational collection, memory is divided into the new generation and the old generation according to the survival time of objects. According to the survival characteristics of objects in each generation, different garbage collection algorithms are used for each generation. The new generation adopts mark-copy algorithm and the old generation adopts mark-collation algorithm.
The implementation of the garbage algorithm involves a great deal of procedural detail, and different virtual machine platforms implement it in different ways. The above is just the basic idea.