In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the concept of JVM memory generation and garbage collection". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the concept of JVM memory generation and garbage collection".
JVM memory area
We all know that the memory area of JVM is divided into five parts. If you are in doubt, you can refer to a previous article-introduction to JVM memory region.
Here is also a brief list of the five parts of JVM
Program counter this is a small piece of memory space, its function can be seen as the current thread executed bytecode line number indicator, thread private.
Java virtual machine stack is the memory model of Java method execution. Each method is called to the process of execution completion, corresponding to a stack frame in the virtual machine stack from the stack to the stack process, thread private.
The local method stack is similar to the virtual machine stack, except that the local method stack is used to execute local methods, and threads are private.
The only purpose of this area of the Java heap is to store objects. Almost all object instances in the application are allocated memory here and shared by all threads.
The method area is used to store class information, constants, static variables, compiled code and other data that have been loaded by the virtual machine, which are shared by all threads.
About OOM
As we all know, after any application is started, the memory allocated to it by the operating system must be limited, so how to manage memory reasonably and effectively becomes particularly important.
As you can see from the previous section, the object memory allocation we generally discuss takes place on the Java heap. So in most cases, memory management here refers to Java heap memory. While the program counter, virtual machine stack, they are born and die with the thread, so their memory is relatively easy to manage, and there are fewer problems.
After an application starts, it keeps running, executing commands and creating objects, and most of these objects are stored in the heap memory area. The size of this part of the area is limited, and the objects that need to be generated are unlimited. When you create an object and find that there is really no space in heap memory to create an object, JVM will expose an OutOfMemoryError exception (hereinafter referred to as OOM) and the program will die.
It just shows the appearance. In fact, OOM is far from that simple as mentioned above. Here are some other things to explain if you want to understand OOM.
Before OOM occurs, JVM actually performs memory garbage collection (GC).
There are many different implementation algorithms for garbage collection.
In order to better manage memory, heap memory is divided into generations.
The new generation of heap memory is inconsistent with the old garbage collection algorithms.
In fact, the knowledge here requires a comprehensive understanding, you will have a comprehensive understanding of OOM.
Memory generation
When an application starts, the operating system will assign him an initial memory size. As can be seen from the above, most of this memory should belong to heap memory. JVM has divided this area in order to make better use of this memory. One part becomes the new generation, and the other part is called the old age.
At the beginning, the creation of objects takes place in the new generation. With the continuous creation of objects, if the new generation has no space to create new objects, GC will occur. At this time, the GC is called Minor GC. Every time the object in the new generation passes through Minor GC, if the object is not recycled, add 1 to its own number of tags. This number of tags is used to identify how many times the object has experienced Minor GC, for Sun's Hotspot virtual machine. If this number exceeds 15, the object will be moved to the old age.
With the passage of time, if the old age does not have enough space to accommodate the object, the old age will also try to launch GC, which is called Full GC.
Compared with Minor GC,Full GC, there are fewer occurrences, but each time Full GC occurs, the entire heap memory area needs to be garbage collected, which has a much greater impact on program performance than Minor GC. Therefore, we should try our best to avoid or reduce the occurrence of Full GC.
At the same time, in the heap memory area, the most common occurrence of GC is the new generation of Minor GC, because all objects will give priority to the new generation to open up space, so this piece of memory will change very quickly, only when there is not enough memory, GC will occur, but the general Minor GC execution is much faster than Full GC. Why? Because the garbage collection algorithm of the new generation is different from that of the old era.
Garbage collection algorithm tag-cleanup algorithm (Mark-Sweep)
This is the most basic collection algorithm, as its name is, and the algorithm is divided into two phases: "tag" and "clear":
First of all, all the objects that need to be recycled are marked, and all the marked objects are recycled uniformly after the marking is completed.
The reason why it is the most basic collection algorithm is that the subsequent collection algorithms are based on this idea and improve its shortcomings.
It has two main disadvantages: one is the efficiency problem, the efficiency of the marking and removal process is not high; the other is the space problem, after mark removal will produce a large number of discontinuous memory fragments, too much space debris may lead to, when the program needs to allocate larger objects in the future, it can not find enough continuous memory and has to trigger another garbage collection action in advance.
Replication algorithm (Copying)
To solve the efficiency problem, a collection algorithm called Copying has emerged, which divides available memory into two equal-sized chunks of capacity, using only one of them at a time. When this piece of memory is used up, copy the surviving objects to another piece, and then clean up the used memory space at once.
In this way, one of the blocks is reclaimed every time, and the complex situations such as memory fragments do not have to be taken into account when allocating memory. As long as the pointer at the top of the stack is moved and the memory is allocated sequentially, the implementation is simple and efficient. But the cost of this algorithm is to reduce the memory to half of the original, which is a bit too high.
However, the efficiency of this algorithm is quite high, so today's commercial virtual machines use this collection algorithm to recover the new generation. Why can the new generation use replication algorithms?
A special study by IBM shows that 98% of objects in the new generation die overnight, so there is no need to divide memory space according to the proportion of 1:1. In view of this, the new generation has adopted the following division strategy.
The Cenozoic era is now divided into three parts, a larger Eden (Garden of Eden) and two smaller Survivor (survivors) areas.
When recycling, copy the living objects in Eden and Survivor to another piece of Survivor space at once, and finally clean up the space of Eden and the Survivor you just used. The default size ratio of Eden to Survivor for HotSpot virtual machines is 8 ∶ 1, which means that the available memory space in each Cenozoic generation is 90% (80% to 10%) of the entire Cenozoic capacity, and only 10% of the memory is "wasted".
After this cleanup, the original Survivor is empty and remains empty until the next time the Minor GC is used as a place for living objects. In this way, the two Survivor take turns to act as transit stations for the new generation of surviving objects in the GC process.
However, if the memory area using the replication algorithm has a large number of living objects, the replication algorithm will become stretched, and a larger Survivor area is needed to hold those living objects, perhaps even 1:1. So for the old days of the heap memory area, there is the following algorithm.
Marking-finishing algorithm
The marking process is still the same as the mark-clear algorithm, but the next step is not to clean up recyclable objects directly, but to move all surviving objects to one end, and then directly clean up memory beyond the end boundary. This method avoids the generation of fragments and does not require an extra piece of memory space, which is more suitable for old times.
But compared with the replication algorithm, although this algorithm takes up less memory space, it takes longer to collect garbage than the replication algorithm, so it is also mentioned above.
We should try our best to avoid or reduce the occurrence of Full GC.
These two algorithms are described in concise language
Replication algorithm: trade space for time
Mark-organize algorithm: trade time for space
In a word, you can't have both fish and bear's paw, but for the new generation and the old age, they are the best choice.
Thank you for reading, these are the contents of "what is the concept of JVM memory generation and garbage collection". After the study of this article, I believe you have a deeper understanding of what the concept of JVM memory generation and garbage collection is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.