In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to optimize online JVM". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
What does the JVM memory model look like?
JVM memory model can be roughly divided into thread private area and shared area. Thread private area consists of virtual machine stack, local method stack and program counter, while shared area consists of heap and metadata space (method area).
If someone asks you about the JVM memory model, recall the diagram above, but it is still not enough to know what the JVM memory model looks like, but also to know what they do.
Virtual machine stack/native method stack
When you encounter StackOverflowException, have you ever thought about why this exception occurs? The answer lies in the VM stack, where the JVM generates a stack frame for each method and pushes the stack frame into the VM stack.
Take a corn: Assuming the JVM parameter-Xss is set to 1m, if a method creates a 128kb array, then the method can only recurse 4 times in the same thread, and then the fifth recursion will report StackOverflowException, because the virtual machine stack size is only 1m, each recursion needs to allocate 128kb space in the virtual machine stack for the method, which shows that there is insufficient space for the fifth recursion.
program counter
A program counter is a line number indicator that records the bytecode executed by the current thread. JVM multithreading is implemented through CPU time slice rotation (i.e. threads switch in turn and allocate processor execution time) algorithms. That is, one thread may be suspended during execution because the time slice runs out, while another thread acquires the time slice to begin execution.
Simply put, the main function of a program counter is to record the line number indicator of the bytecode executed by the current thread.
Method area (metadata area)
Method area stores metadata information, static variables, constants and other data of the class.
heap
Usually, objects created by using the new keyword will enter the heap. The heap is also the area that GC focuses on. The heap will be divided into: Cenozoic, Old Age, and Cenozoic will be further divided into Eden area and Survivor area:
The Eden area and Survivor area in the Cenozoic generation are based on the JVM recycling algorithm, but most of them are now using the generational recycling algorithm, so when introducing the heap, the Cenozoic generation will be directly summarized as Eden area and Survivor area.
summary
JVM memory model summary:
JVM memory model is divided into thread private area and shared area
Virtual machine stack/native method stack is responsible for storing thread execution method stack frames
The program counter is used to record the position of the thread execution instruction
Method area (metadata area) stores metadata information, static variables, constants and other data of the class
Heap Objects created with the new keyword all go into the heap, which is divided into new generation and old generation.
When can an object be reclaimed?
There are two ways for JVM to determine object reclamation: reference count and GC Roots. Reference count is relatively simple. JVM maintains a reference count for each object. Assuming that the reference count of object A is zero, it means that no task object references object A, so object A can be reclaimed. However, reference count has a disadvantage that it cannot solve the problem of circular reference.
GC Roots uses a series of objects named GC Roots as a starting point, and searches downward from these nodes. The searched path is called a reference chain. When an object is not connected to GC Roots by any reference chain, it proves that the object is unavailable.
In Java, objects that can be GC Roots include the following:
Objects referenced in the virtual machine stack;
Objects referenced by static attributes of classes in the methods section;
Objects referenced by constants in the method section;
Objects referenced by JNI (i.e., Native methods in general) in the native method stack;
summary
Generally speaking, when an object cannot be searched through GC Roots, it means that the object can be recycled, but when to recycle depends on GC's mood!
What are the common garbage collector algorithms and what are their advantages and disadvantages? mark clear
This algorithm is divided into two parts: marking, cleaning two stages,
The marking phase starts scanning from the GC Root, marking each object as alive when it arrives, and the clearing phase clears unmarked objects after scanning is completed.
A flaw in this algorithm is that it creates memory fragmentation, as shown in Figure B above. After being cleared, it will leave a memory area. If large objects need to be allocated later, there will be no continuous memory available.
marking arrangement
Tag defragmentation is no longer a problem. It also scans from the GC Root to mark and then cleans up useless objects. After cleaning up, it will defragment the memory.
This makes memory continuous, but another problem arises: you have to move objects every time, so the cost is high.
replication algorithm
The replication algorithm will split the JVM into two equal parts. If the heap is set to 1g, the heap will be divided into two regions of 512m each when the replication algorithm is used. When allocating memory to objects, GC always uses one of them to allocate memory. When it is full, GC will mark it, then move the surviving objects to another empty area, and then clear all the objects that are not surviving. In this way, repeated processing will always have a blank area that is not properly utilized.
The biggest problem with alternating use of the two regions is that it will lead to a waste of space, and now the heap memory utilization rate is only 50%.
summary
JVM Recycling Algorithm Summary:
Mark clearing speed is fast, but will produce memory fragmentation;
Tag defragmentation solves the problem of tag defragmentation, but it is expensive to move objects every time;
Replication algorithms have no memory fragmentation and do not need to move objects, but lead to waste of space;
When does the subject enter old age?
Newly created objects will initially stay in the new generation, but as the JVM runs, some of the surviving long objects will slowly move into the old generation.
Based on age
The JVM increments an age counter for an object,+1 for each GC the object "survives," and moves the object to an older age when it reaches a set threshold (15 by default), which can be adjusted by-XX:MaxTenuringThreshold.
After a Minor GC, the age of the object is +1, objects that reach the threshold are moved to the old age, and other surviving objects remain in the Cenozoic generation.
dynamic age judgment
There is another strategy based on the age of the object that will also allow the object to enter the old generation without waiting for 15 GC to enter the old generation. His general rule is that if the current Survivor of the object, the total size of a batch of objects is greater than 50% of the Survivor memory, then objects older than this batch of objects can directly enter the old age.
If Survivor 2 is 100m, if A + B + D has a memory size greater than 50m, and D is now 10 years old, then E will be moved to the old age. In fact, this calculation logic is like this: if the sum of multiple objects of age 1 + age 2 + age n exceeds 50% of the Survivor zone, then all objects above age n will be placed in the old age.
Large objects go directly into older generations
If the parameter-XX:PretenureSizeThreshold is set, then if you want to create an object larger than the value of this parameter, such as allocating a very large byte array, then you will directly put this large object into the old age, without passing through the new generation.
Doing so can avoid large objects in the new generation, repeatedly avoid GC, but also have to copy them to copy, and finally enter the old generation, such a large object to copy back and forth, is very time-consuming.
What is a space allocation guarantee strategy?
JVM Before a Minor GC occurs, the VM checks whether the maximum available contiguous space in the old generation is greater than the total space of all objects in the new generation, and if so, the Minor GC is safe; if so, the VM checks whether the HandlePromotionFailure setting allows warranty failure. If HandlePromotionFailure=true, it will continue to check whether the maximum available continuous space in the old age is larger than the average size of objects promoted to the old age. If it is larger than the average size, it will try to perform a Minor GC, but this Minor GC is still risky; if it is smaller than or HandlePromotionFailure=false, it will perform a Full GC instead.
How to optimize reduction of Full GC?
Summarizing some of the previous questions and then applying them to the line, how should the JVM optimize for reducing Full GC? Taking the standard 4-core 8G machine as an example, the system reserves 4G first, and the other 4G is allocated according to the following rules:
Heap memory: 3g
Cenozoic: 1.5g
Cenozoic Eden Zone: 1228m
Cenozoic Survivor Zone: 153m
Method area: 256m
Virtual machine stack: 1m/thread
Set parameters as follows:
-Xms3072m-Xmx3072m-Xmn1536m-Xss=1m-XX:PermSize=256 m-XX:MaxPermSize=256 m-XX:HandlePromotionFailure-XX:SurvivorRatio=8 Estimate how much memory the system consumes per second
Before optimizing the JVM, you must first estimate the amount of memory to be occupied by the system per second. For example, if there is a shopping mall system with a daily life of one million, the daily order volume is about 20w. According to the calculation of 8 hours a day, there will be about 500 requests per second for the order service, and then roughly estimate how much memory is occupied by each request and calculate how much memory is spent per second.
Assuming 500 requests per second, each request requires 100k of space to be allocated, which requires approximately 50m of memory to be allocated per second.
Calculate how often Minor GC is triggered
According to the previous estimate, about 50m of memory needs to be allocated for 1 second, and the space of Eden area is 1228m. On average, Minor GC will be performed every 25 seconds.
Check whether the Survivor Zone is sufficient.
According to the above model, Minor GC will be executed every 25 seconds. During GC execution, all objects in the new generation cannot be recovered. If 50m per second is executed, there will be about 100m objects that cannot be recovered during each GC execution. The Survivor area will be entered. However, don't forget that the JVM has a dynamic age determination mechanism. The space for Survivor is obviously a little smaller, so set the new generation to 2048m to avoid triggering dynamic age determination:
-Xms3072m-Xmx3072m-Xmn2048m... Large objects go directly into older generations
Large objects are generally long-term survival and use objects. Generally speaking, objects with 1M are set to enter the old generation directly, so as to avoid large objects being copied back and forth in the new generation, so PretenureSizeThreshold=1m parameter is added.
...- XX:PretenureSizeThreshold=1m... Set age threshold reasonably
After Minor GC, it will automatically advance to the old generation after avoiding 15 garbage collections by default. According to our evaluation, Minor GC will be triggered once every 25 seconds. If the default value of MaxTenuringThreshold parameter is used, it should be 6 minutes later after avoiding 15 GC. Combined with the current business scenario, it can be reduced a little so that those objects that should have entered the old generation will enter the old generation as soon as possible to avoid replication costs and waste of new generation space, resulting in insufficient space for new generation Survivor and triggering Full GC.
...- XX:MaxTenuringThreshold=6... "How is the online JVM optimized" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.