Garbage Collection is often referred to as “GC”. In the JVM, program counters, virtual machine stacks, and local method stacks are created and removed with the thread. Stack frames are loaded and unloaded as methods enter and exit, realizing automatic memory cleaning. Our memory garbage collection is concentrated in the Java heap and method areas, where memory is allocated and used dynamically during program execution!

Since is recycling, that we certainly want to know what is right, I believe that one day down and Java development yichun the little brother to pick up trash for living, I breathe, a juo, bend, open lid, pour water, with my eyes closed, the guangdong handsome didn’t drink happy fat curtilage water when the garbage pick up wrong, the guangdong handsome also may hit persons? Yes, and the premise of garbage collection for the JVM is to know what garbage is! The JVM first runs a series of counting algorithms to determine if it is garbage. If an object is alive, it is not garbage and cannot be collected. Instead, it can be collected.

1. Two counting algorithms for the JVM to determine whether an object is alive or not

There are two main counting algorithms for the JVM to judge whether an object is alive or not, which are reference counting and reachability analysis counting algorithm.

As soon as I heard the word “algorithm” in the counting algorithm, the little white boy’s shoes became stunned: Mom, math is not good, finished, do not learn… Cough cough cough, zha people do not mention the algorithm, from the zha People’s Daily life began, in our peacetime a thing is often not used, but also do not use what place, so this thing can be said to be garbage. In Java, if an object cannot be referenced again, it is garbage and should be recycled. That’s how the JVM’s two counting algorithms work. It’s really not complicated.

1.1 reference counting algorithm

Based on our thinking in life, we can easily think of a reference counting method to determine garbage. By adding one when an object is referenced and subtracting one when it is dereferenced, we can determine whether an object is garbage by determining whether the reference count is zero. This method is commonly referred to as reference counting. It is true that a simple idea is flawed because it is simple, so the reference counting algorithm is defined this way

1. Reference count: Each object has a reference count attribute. When a new reference is added, the count is increased by 1; when a reference is released, the count is decreased by 1. 2. Because of the simplicity of the method, reference counting algorithm cannot solve the problem of objects referencing each other circularly.

Little white kid probably doesn’t know what an object circular reference is, bear dei looks at the picture

1.2. Counting algorithm for accessibility analysis

First of all, the actual development languages such as Java, C# and so on are using reachability analysis counting algorithm to determine whether the object is alive or not!

The definition of the accessibility analysis algorithm is as follows:

Reachability Analysis: Start with GC Roots and search down the path called the reference chain. When an object is not connected to GC Roots by any reference chain, the object is proved to be unavailable. Unreachable objects.

GC Roots is a bit abstract, so what is GC Roots? In the Java language, GC Roots mainly includes:

Object reference, located in the vm stack. Static references in the method area. References to JNI in the local method stack. In short, GC Root is a collection of carefully selected references

Garbage collection algorithms

Now that’s an overview of garbage, let’s talk about how to recycle it! In our daily life, we can collect garbage and sweep it as we walk, or we can sweep it all together and clean it again. In the JVM, like life, a series of garbage collection algorithms are provided.

And we also often can see garbage classification, divided into recyclable waste, and recycle trash, but from a certain sense, also are rubbish, if everybody obey the rules of the garbage sorting, I estimate later recyclers of uncle aunt every day from recycled bin laid hands on him, in fact in the JVM also like this, the JVM took the partition, the thought of the generational collection.

Common garbage back algorithms: tag cleaning algorithm, copy algorithm, tag collation algorithm, generational collection algorithm.

2.1 Mark clearing algorithm Replication algorithm

Tag clearing algorithm. As can be seen from the name, it is divided into two phases: the mark phase and the sweep phase. One possible implementation is to mark all reachable objects triggered by GC Root during the marking phase. At this point, all unmarked objects are junk objects. Then, in the purge phase, all unmarked objects are cleared. The biggest problem of tag – clearing algorithm is space debris. If there is too much space debris, the memory space can be discontinuous. Although large objects can be allocated in discontinuous space, they are less efficient than contiguous memory space.

2.2 Copying Algorithms

Copying algorithms. The core idea of the replication algorithm is to divide the original memory space into two equal pieces, and use one piece at a time. During garbage collection, the living objects in the used memory are copied to the unused memory block. All objects in the memory block in use are then cleared, and the roles of the two memory blocks are swapped to complete garbage collection. The disadvantage of the replication algorithm is that half of the memory space is wasted. So now commercial virtual machines are using this replication algorithm for the next generation of garbage collection! Because when the object survival rate is high, the replication algorithm becomes inefficient

2.3 Mark-Compact Algorithm

Another name for the mark-collation algorithm is the tag compression algorithm, and it’s good to know that it means the same thing.

Tag collation algorithm. The tag sorting algorithm can be said to be the optimized version of the tag clearing algorithm, which also needs to go through two stages, namely, the tag stage and the tag sorting stage. In the marking phase, all objects are triggered from the COLLECTION of GC Root references to mark, just as in the marking phase of the marking cleanup algorithm. In the collation phase, all surviving objects are collated to one side of memory, and then all space outside the boundary is cleaned up. Therefore, mark-compact does not defragment memory, but spends more time on Compact!

2.4 Generational Collection

A “Generational Collection” algorithm divides the Java heap into Generational and older generations so that the most appropriate Collection algorithm can be used for each generation. In the new generation, a large number of objects are found dead and only a small number of objects survive each garbage collection. Then the new generation chooses the replication algorithm and only needs to pay a small amount of the replication cost of the surviving objects to complete the collection. However, in the old age, because the object has a high survival rate and there is no extra space for its allocation guarantee, the old age uses “mark-clean” or “mark-tidy” algorithm to recycle.

2.5. Summary of four recycling algorithms

Mark clearing algorithm

It is divided into marking stage and clearing stage. The biggest problem of tag – clearing algorithm is space debris. It is suitable for a large number of viable objects.

Copying algorithms

The disadvantage of the replication algorithm is that half of the memory space is wasted. Commercial virtual machines today use this replication algorithm for the next generation of garbage collection! Because when the survival rate of objects is high, the replication algorithm becomes inefficient and is more suitable for the situation where there are fewer viable objects.

Mark-compact Algorithm

Mark-compact is an optimized version of the mark-compact algorithm. Mark-compact does not defragment memory, but spends more time on Compact!

Generational Collection

The new generation uses the replication algorithm, while the old generation uses the “mark-clean” or “mark-tidy” algorithm to recycle.

3. Summary of garbage collection algorithm

Borrow a picture that big guy arranges:

If this article helped you at all, please give it a thumbs up. Thanks

Finally, if there is insufficient or improper place, welcome to criticize, grateful! If you have any questions welcome to leave a message, absolutely the first time reply!

Welcome to pay attention to my public number, there are some Java learning materials and a large wave of Java electronic books, such as Zhou Zhiming teacher’s in-depth Java virtual machine, Java programming thought, core technology volume, big talk design mode, Java concurrent programming combat….. Are Java bible, don’t say fast on the Tomcat car, how we go! The most important is to discuss technology together, yearning for technology, the pursuit of technology, good come is a friend oh…