In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "introduction to the working principle of Java GC". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to the working principle of Java GC".
The JVM memory structure consists of heap, stack, local method stack, method area, and so on. The structure diagram is as follows:
1) heap
The memory of all objects created through new is allocated in the heap, and its size can be controlled by-Xmx and-Xms. The heap is divided into Cenozoic and Paleozoic, and the Cenozoic is further divided into Eden and Survivor regions. * Survivor is composed of FromSpace and ToSpace. The structure diagram is as follows:
The new generation. The new objects are allocated memory with the new generation. When the Eden space is insufficient, the surviving objects will be transferred to the Survivor. The size of the new generation can be controlled by-Xmn, or the ratio of Eden to Survivor can be controlled by-XX:SurvivorRatio. Used to store objects in the new generation that are still alive after multiple garbage collection
2) Stack
When each thread executes each method, it requests a stack frame in the stack. Each stack frame includes a local variable area and an Operand stack, which is used to store temporary variables, parameters, and intermediate results during the method call.
3) Local method stack
Used to support the execution of native methods, storing the state of each native method call
4) method area
Stores the class information to be loaded, static variables, constants of type final, properties, and method information. JVM uses persistent generation (PermanetGeneration) to store the method area, and you can specify minimum and * * values through-XX:PermSize and-XX:MaxPermSize. After introducing the JVM memory structure, let's take a look at the JVM garbage collection mechanism.
JVM garbage collection mechanism
JVM adopts different garbage collection mechanisms for the new generation and the old generation respectively.
The new generation of GC:
The new generation usually has a short survival time, so it is based on the Copying algorithm to recycle. The so-called Copying algorithm is to scan the living objects and copy them to a new completely unused space, corresponding to the new generation, that is, copy between Eden and FromSpace or ToSpace. The new generation uses free pointers to control the GC trigger. The pointer maintains the position of an allocated object in the new generation. When there is a new object to allocate memory, it is used to check whether there is enough space to trigger GC. When objects are allocated continuously, the objects gradually move from eden to survivor,*** to the old generation
Using javavisualVM to view, you can clearly see that when the new generation is full, the object will be transferred to the old generation, and then emptied to continue loading. When the old generation is also full, an exception of outofmemory will be reported, as shown in the following figure:
In the execution mechanism, JVM provides serial GC (SerialGC), parallel recycling GC (ParallelScavenge) and parallel GC (ParNew).
1) Serial GC
Single thread is used in the whole scanning and copying process, which is suitable for applications with single CPU, small new generation space and not very high pause time requirements. It is the default GC mode at client level, and can be specified by-XX:+UseSerialGC.
2) parallel recovery of GC
The whole scanning and replication process is carried out in a multi-threaded way, which is suitable for applications with multi-CPU and short pause time. It is the default GC mode at the server level, which can be specified by-XX:+UseParallelGC and the number of threads by-XX:ParallelGCThreads=4.
3) parallel GC
Used in conjunction with concurrent GC of the old generation
GC of the Old Age:
The old generation is different from the new generation, the survival time of objects is longer and more stable, so the Mark algorithm is used to recycle. The so-called mark is to scan the surviving objects, and then recycle the unmarked objects. After recovery, the vacant space is either merged or marked for next allocation. In short, it is necessary to reduce the efficiency loss caused by memory fragments. In the execution mechanism, JVM provides serial GC (SerialMSC), parallel GC (parallelMSC) and concurrent GC (CMS), but the details of the algorithm need to be further studied.
The above GC mechanisms need to be used in combination, as shown in the following table:
At this point, I believe you have a deeper understanding of the "introduction to the working principle of Java GC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.