In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the problems related to Java GC". In daily operation, I believe many people have doubts about what problems related to Java GC. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the questions related to Java GC?" Next, please follow the editor to study!
Java GC
target
When we encounter a problem or a knowledge point, we should understand and understand what problem is to be solved. Speaking of Java GC, what is the purpose of this GC? It is obvious that memory is recycled because memory is limited, and as more and more objects are created in the program, the memory will become larger and larger if recycled, and the program will end up with an exception. Since the goal is to reclaim memory, the new question arises: which objects can be recycled? When will it be recycled? How to recycle it?
Which objects can be recycled
To put it simply, useless objects can be recycled, in other words, what if defining an object is useless? There are two main methods, one is called citation counting method, and the other is called reachability analysis.
Reference count
Reference counting means that if an object is referenced once by another object, the object will have a reference counter, which will be incremented by one; if released, the reference counter will be reduced by one. When the count of the reference counter is 0, it means that the object is useless, and the object can be recycled. On the surface, it seems reasonable and convenient to implement, but if you think about it, you will find that there is something wrong with it. Since the problem of circular reference, for example, object A refers to object B, but object B also references object A, then the reference counter of object An and object B will not be 0. but neither of these objects is referenced by other objects, and theoretically both objects can be recycled.
As you can see from the above, this scheme is problematic and can lead to memory leaks. Then there is another scheme, but it is accessibility analysis.
Accessibility analysis
Reachability analysis refers to searching down from the point of GCRoots as a starting point, indicating that the object is junk when no reference chain is found. So which objects can be thought of as Roots nodes? There are objects in the Java stack, static properties and constants in the method area, and objects in the local method stack. Search down from these objects in turn, and if the object that does not reach the Roots node is junk, it can be recycled.
All of the following figure shows that the object An and Roots can find the connection with the DEF node, but the three objects of the DEF cannot find the connection with the Roots node, that is, they are unreachable, so the three objects are junk objects.
When will it be recycled
The above two solutions solve which objects can be recycled, so the next question is when to carry out garbage collection. When excluding human calls, garbage collection occurs when memory allocation is made for new objects. If there is not enough memory space, GC will be triggered for garbage collection.
How to recycle
Above we know which objects can be recycled and when we should recycle, so the next thing to solve is how to carry out garbage collection. Garbage collection has a variety of different algorithms depending on the way it is implemented. For example, there are tag removal algorithm, replication algorithm, tag finishing algorithm, generation recovery algorithm, the following is a brief introduction, you can study it yourself if you want to know more.
Label removal algorithm
The tag cleanup algorithm is easy to understand, mainly to perform two actions, one is the tag, and the other is to clear and reclaim the memory of the marked object. The problem with this algorithm is that there will be serious memory fragmentation. As shown in the following figure:
As you can see from the figure above, there is severe memory fragmentation after memory collection, which results in insufficient memory when allocating some large objects, but the overall memory is sufficient.
Replication algorithm
The implementation of the replication algorithm is relatively simple and clear, that is, the memory is divided into two parts, only one of which is fixed in normal use, and when GC is needed, copy the surviving objects to another part, and then clean up all the memory that has been used. As shown below:
From the figure above, you can see that the memory fragmentation problem of mark removal has been solved, but it is obvious that another problem with the replication algorithm is that the memory usage is greatly reduced, and the memory that can be used is only half of what it used to be.
Tag finishing algorithm
Since tag removal and replication algorithms have their own advantages and disadvantages, it is natural for us to think about whether the two algorithms can be combined, so there is a tag finishing algorithm. The marking phase is the same as the mark clearing algorithm, which first marks the parts that need to be recycled, but the cleanup phase does not clean up directly, but moves the surviving objects to one end of memory, and then clears the rest. As shown below:
Although the tag finishing algorithm can solve some of the problems of the above two algorithms, it still needs to be marked first and then moved, and the overall efficiency is still on the low side.
Generation recovery algorithm
Generation recovery algorithm is a widely used algorithm at present, this is not a new algorithm, but only the division of memory, different regions of memory use different algorithms. According to the survival time of the object, the memory is divided into the Cenozoic era and the old age, in which the Cenozoic era includes the Eden region and S0Magi S1. In the new generation, the replication algorithm is used. Only Eden and S0 regions are used for object memory allocation. When GC occurs, the surviving objects are copied to S1 area, and then replicated repeatedly. When an object is still alive after 15 times of GC, the object will enter the old age. In the old days, because there were fewer objects to be recycled each time, the tagging algorithm was used.
Garbage collector
After talking about the garbage collection algorithm, let's take a look at the garbage collector. Each kind of garbage collector is a different product of different times and has its own uniqueness.
Serial garbage collector (single thread, replication algorithm)
ParNew garbage Collector (Serial+ multithreading)
Parallel Scavenge collector (multithreaded replication algorithm, efficient)
SerialOld collector (single-threaded tag finishing algorithm)
ParallelOld collector (multithreaded tag finishing algorithm)
CMS collector (multithreaded tag removal algorithm)
G1 collector
The coordinated use of various garbage collectors can refer to the following figure. I don't think it is necessary to be proficient in so many collectors. You can pay attention to CMS and G1. Interested friends can do some research on their own.
At this point, the study of "what are the problems related to Java GC" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.