In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to tune Java memory and garbage collection. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
To understand the Java garbage collection mechanism, it is important to understand the JVM memory pattern first. Today we will look at the various parts of JVM memory, how to monitor, and how to tune garbage collection.
Java (JVM) memory model
As you can see from the picture above, JVM memory is divided into separate parts. Broadly speaking, JVM heap memory is divided into two parts-the younger generation (Young Generation) and the older generation (Old Generation).
Young generation
The younger generation is the place where all new objects are born. Garbage collection will be triggered when the lightweight memory space is used up that year. This garbage collection is called Minor GC. The younger generation is divided into three parts-Enden region and two Survivor regions.
The main points of the younger generation space:
Most newly created objects are located in the Eden zone.
Minor GC is executed when the Eden area is filled with objects. And transfer all the surviving objects to one of the survivor areas.
Minor GC also examines the surviving objects and moves them to another survivor area. In this way, there is always an empty survivor area for a period of time.
After several GC cycles, objects that are still alive are transferred to the older generation memory space. This is usually done by setting an age threshold before the younger generation is eligible for promotion to the older generation.
The old generation
The memory of the older generation contains objects that have survived for a long time and objects that have survived multiple Minor GC. Garbage collection is usually carried out in the old days when memory is full. Garbage collection in the old days was called Major GC. Major GC will take more time.
Stop the World event
All garbage collection is a "Stop the World" event because all application threads stop until the operation is complete (so it's called "Stop the World").
Because the objects in the younger generation are short-lived objects, the execution of Minor GC is very fast, so the application will not be affected by ("Stop the World").
Because Major GC examines all living objects, it takes longer. Major GC should be reduced as much as possible. Because Major GC can slow your application during garbage collection, if you have an application that requires a quick response to Major GC multiple times, you will see a timeout error.
Garbage collection time depends on the garbage collection strategy. This is why it is necessary to monitor and tune garbage collection. So as to avoid timeout errors in applications that require fast response.
* generation
The * generation or "Perm Gen" contains the application metadata required by JVM, which describes the classes and methods used in the application. Note that the * generation is not part of the Java heap memory.
* to store the classes used by the JVM runtime. The * * generation also contains the classes and methods of the Java SE library. The object of * generation performs garbage collection during full GC.
Method area
The method area is part of the generation space and is used to store type information (runtime constants and static variables) and method code and constructor code.
Memory pool
If the JVM implementation supports it, JVM memory management creates a memory pool for immutable objects. String pooling is a good example of a memory pool type. Memory pools can belong to heaps or generations, depending on the implementation of JVM memory management.
Running constant pool
The runtime constant pool is the runtime representative of each class constant pool. It contains the runtime constants and static methods of the class. The run-time pool is part of the method area.
Java stack memory
Java stack memory is used to run threads. They contain temporary data in the method and specific data referenced by other objects in the heap. You can read the difference between stack memory and heap memory.
Java heap memory switch
Java provides a large number of memory switches (parameters) that we can use to set the memory size and their ratio. Here are some common switches:
VM switch VM switch description-Xms sets the initialization size of the heap when JVM starts. -Xmx sets the heap * * value. -Xmn sets the space size of the younger generation, and the rest is the space size of the old age. -XX:PermGen sets the initialization size of memory generation. -XX:MaxPermGen sets the * * value of * * generation. -XX:SurvivorRatio provides the space ratio of the Eden zone and the survivor zone. For example, if the size of the younger generation is 10m and the VM switch is-XX:SurvivorRatio=2, then 5m of memory will be reserved and 2.5m of memory will be allocated to Eden and each Survivor. The default scale is 8. XX:NewRatio provides the ratio of the older generation to the younger generation. The default value is 2.
Most of the time, the above options are sufficient for Java garbage collection. But if you want to know more about other options, please check out the JVM options official page.
Java garbage collection finds useless objects, removes it from memory and releases memory for later created objects to use. One of the advantages of Java programming languages is automatic garbage collection, which does not require manual allocation and release of memory like other programming languages, such as C.
The garbage collector is a background running program. It manages all objects in memory and finds objects that are not referenced. All these unreferenced objects are deleted, their space is reclaimed and allocated to other objects.
A basic garbage collection process involves three steps:
Mark: this is the * step. In this step, the garbage collector finds out which objects are in use and which are not.
Normal cleanup: the garbage collector cleans up objects that are not in use and reclaims their space for other objects.
Compression cleanup: to improve performance, compression cleanup moves all living objects together after deleting useless objects. This improves the efficiency of allocating new objects.
There are two problems with simple marking and clearing methods:
It's inefficient. Because most new objects will become "useless objects".
Objects that have gone through multiple garbage collection cycles are likely to survive later cycles.
The problem with the simple cleanup method above is that Java garbage collection is recycled from generation to generation, and there are two areas of heap memory: the younger generation and the older generation. I have explained above how Minor GC and Major GC scan objects and how to move objects from one generation space to another.
Java garbage collection type
Here are five types of garbage collection that can be used in applications. Just use the JVM switch to enable the garbage collection policy in our application. Let's learn one by one:
SerialGC (- XX:+UseSerialGC): SerialGC uses simple marking, clearing, and compression methods to garbage collect the younger and older generations, namely Minor GC and Major GC. Serial GC is useful in client mode (client mode), such as in simple stand-alone applications and machines with low CPU configurations. This mode works well for applications with less memory.
ParallelGC (- XX:+UseParallelGC): ParallelGC and Serial GC are almost the same except that N threads are generated for the younger generation of garbage collection. The N here is the kernel number of the system CPU. We can use the JVM option-XX:ParallelGCThreads=n to control the number of threads. The parallel garbage collector is also called the throughput collector. Because it uses multiple CPU to speed up garbage collection performance. Parallel GC uses a single thread for older generation garbage collection.
ParallelOldGC (- XX:+UseParallelOldGC): same as Parallel GC. The difference is that Parallel Old GC uses multithreaded collection for both the younger generation and the older generation of garbage collection.
Concurrent tag cleanup (CMS) collector (- XX:+UseConcMarkSweepGC): the CMS collector is also known as a temporary pause concurrent collector. It collects garbage for the elderly. The CMS collector performs garbage collection through multi-thread concurrency to minimize the pause caused by garbage collection. The CMS collector uses the same algorithm for garbage collection for the younger generation as the Parallel collector. This garbage collector is suitable for applications that cannot tolerate long pauses for quick response. You can use the-XX:ParallelCMSThreads=n JVM option to limit the number of threads in the CMS collector.
G1 garbage collector (- XX:+UseG1GC) G1 (Garbage First): the garbage collector is a feature that can only be used after Java 7, and its long-term goal is to replace the CMS collector. The G1 collector is a parallel, concurrent, and incremental compression short pause garbage collector. The G1 collector operates differently from other collectors in that it does not distinguish between the younger generation and the older generation. It divides the heap space into multiple areas of equal size. When doing garbage collection, it gives priority to collecting areas with fewer surviving objects, so it is called "Garbage First". You can find more details in the Oracle Garbage-FIrst collector documentation.
Java garbage collection monitoring
We can use command line and graphical tools to monitor monitoring application garbage collection. For example, I used a demo from the Java SE download page to experiment.
If you want to use the same application, you can download JDK 7 and JavaFX demos and examples from the Java SE download page. The example application I use is Java2Demo.jar, which is located in the jdk1.7.0_55/demo/jfc/Java2D directory. This is only an optional step, and you can run GC monitoring commands to monitor any Java application.
The commands I use to open the demo application are:
Pankaj@Pankaj:~/Downloads/jdk1.7.0_55/demo/jfc/Java2D$ java-Xmx120m-Xms30m-Xmn10m-XX:PermSize=20m-XX:MaxPermSize=20m-XX:+UseSerialGC-jar Java2Demo.jar
Jsat
You can use the jstat command line tool to monitor JVM memory and garbage collection. Standard JDK already comes with jstat, so you don't need to do anything extra to get it.
To run jstat you need to know the process id of the application, you can use the ps-eaf | grep java command to get the process id.
Pankaj@Pankaj:~$ ps-eaf | grep Java2Demo.jar 501 9582 11579 0 9:48PM ttys000 0v21.66 / usr/bin/java-Xmx120m-Xms30m-Xmn10m-XX:PermSize=20m-XX:MaxPermSize=20m-XX:+UseG1GC-jar Java2Demo.jar 501 14073 14045 0 9:48PM ttys002 00.00 grep Java2Demo.jar
As I know from above, my Java application process id is 9582. You can now run the jstat command, as shown below:
Pankaj@Pankaj:~$ jstat-gc 9582 1000 S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT 1024.0 1024.0 8192.0 7933.3 42108.0 23401.3 20480.0 1999 0.9 157 0.274 40 1.381 1.654 1024.0 0.0 8192.0 8026.5 42108.0 23401.3 20480.0 19990.9 157 0.274 40 1.381 1.654 1024.0 1024.0 0.0 80.0 8192 . 0 8030.0 42108.0 23401.3 20480.0 19990.9 157 0.274 40 1.381 1.654 1024.0 1024.0 0.0 0.0 8192.0 8122.2 42108.0 23401.3 20480.0 19990.9 157 0.274 40 1.381 1.654 1024.0 1024.0 0.0 0.0 8192.0 8171.2 42108.0 23401.3 20480.0 19990.9 157 0.274 40 1.381 1.654 1024.0 1024.0 48.7 0 . 0 8192.0 106.7 42108.0 23401.3 20480.0 19990.9 158 0.275 40 1.381 1.656 1024.0 1024.0 48.7 0.0 8192.0 145.8 42108.0 23401.3 20480.0 19990.9 158 0.275 40 1.381 1.656
One argument to the jstat command is the interval between each output. Memory and garbage collection data are printed out every other second.
Let's understand the meaning of each column one by one:
S0C and S1C: this column shows the current size (in KB) of the Survivor0 and Survivor1 regions.
S0U and S1U: this column shows the current usage of the Survivor0 and Survivor1 zones (in KB). Note: at any time, there will always be a Survivor area that is empty.
EC and EU: these columns show the current space size and usage of the Eden area (in KB). Note: the size of the EU is increasing all the time. And as long as the size is close to EC, Minor GC will be triggered and EU will be reduced.
OC and OU: these columns show the current space size and current usage of the older generation (in KB).
PC and PU: these columns show the current space size and current usage of Perm Gen (in KB).
The YGC and YGCT:YGC columns show the number of GC events that occurred in the younger generation. The YGCT column shows the cumulative time spent on GC operations in the younger generation. Note: when the value of EU decreases due to minor GC, both YGC and YGCT on the same line increase.
The FGC and FGCT:FGC columns show the number of Full GC events that have occurred. FGCT shows the cumulative time of the Full GC operation. Note: GC takes much longer to use than the younger generation.
GCT: this column shows the total cumulative time of the GC operation. Note: the total cumulative time is the sum of the time spent in the YGCT and FGCT columns (GCT=YGCT+FGCT).
The advantages of jstat, we can also run jstat on a remote server without GUI. Note: we specify that the sum of S0C, S1C, and EC is 10m through the-Xmn10m option.
Java VisualVM and Visual GC plug-ins
If you want to view memory and GC in GUI, you can use the jvisualvm tool. Java VisualVM is also part of JDK, so you don't need to download it separately.
Run the jvisualvm command on the terminal to start the Java VisualVM program. Once you start the program, you need to install the Visual GC plug-in from the Tools- > Plugins option, as shown in the image below.
After installing the Visual GC plug-in, open the application from the left column and change your perspective to the Visual GC section. You will get details about JVM memory and garbage collection, as shown in the following figure.
Java garbage collection optimization
Java garbage collection tuning should be an option to improve application throughput. When you find that the application performance degrades and times out due to long garbage collection, you should consider Java garbage collection tuning.
If you see java.lang.OutOfMemoryError: PermGen space errors in the log, try using the-XX:PermGen and-XX:MaxPermGen JVM options to monitor and increase PermGen memory space. You can also try using-XX:+CMSClassUnloadingEnabled and see the performance of using the CMS garbage collector.
If you see a lot of Full GC operations, then you should try to increase the memory space of the old days.
Comprehensive garbage collection tuning takes a lot of effort and time, and there are no rigid tuning rules. You need to try different options and compare them to find the one that works best for your application.
This is all the Java memory model and garbage collection content.
The above is how to tune Java memory and garbage collection. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.