In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to tune the performance of JVM, 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.
Tomcat, Jetty, GlassFish, etc., a series of Web containers / application servers, although as containers, provide a Java Web runtime environment to support the operation of Servlet/JSP, etc., but we all know that it is essentially a Java application. Every time the container is started and run, the Java program is run to realize the ability of the Web container.
As a "special" Java application, like other Java applications for tasks, it needs to use JVM, heap, garbage collection, and different heap partition ratios.
Therefore, tuning JVM is essential in tuning the Web container (application server).
There are two main considerations for JVM tuning:
Memory size configuration
Garbage collection algorithm selection
Of course, to be exact, the above two points are not independent of each other, and the size configuration of memory will also affect the efficiency of garbage collection.
Among them, the most important things to do are memory size configuration.
Determine the total memory footprint
Determine the size partition of each generation (Gen) in memory
Memory size configuration
The so-called memory footprint refers to the memory footprint observed when the application runs steadily for a short period of time after startup.
Taking the HotSpot virtual machine as an example, the Java heap has three main spaces:
The new generation, the old age and the eternal generation.
According to the special characteristics of different applications, observe the memory consumption of the application, and if there are a large number of temporary objects that will not be reused, you can adjust the New Gen so that these temporary objects are created in the new generation and recycled when the Minor GC is generated, so that the short-lived objects will not be promoted to the old age, thus avoiding the generation of Full GC in the garbage heap.
Ideally, the short-term survival objects only complete the life cycle in the new generation, which takes less time and effort.
Minor GC recycling is completed, and long-term survival, will be used many times after multiple recycling promoted to the old age, and finally through the Full GC to complete the life cycle.
The adjustment parameters for memory size involved here are:
-Xms
-Xmx
These two parameters are used to configure the starting size and maximum value of the heap. It needs to be observed here to find an appropriate value. Setting too much will lead to memory waste and garbage collection will take too long. For Tomcat, it is common to set the initial and maximum values to the same value, so as to avoid triggering Full GC to expand memory when the initial memory is low.
After setting the heap size, the size ratio of the new generation to the old age should be adjusted according to the characteristics of the object's life cycle.
The parameters involved are:
-XX:NewSize
-XX:NewRatio
-XX:MaxNewSize
-Xmn
The first is to set the initial size of the Cenozoic directly, and the second is to set the Ratio. Too high or too low can cause GC to not work efficiently. After all, Minor GC takes time. The last setting is that the initial value and the maximum value of the new generation are the same, and the change of heap space does not affect its value.
For applications that use a large number of third-party class libraries, many framework-dependent classes will be loaded, and OOM due to insufficient Perm Gen may be encountered in the process of use. In this case, you can observe the occupation of the Perm area in the steady state, and then set the parameters.
-XX:PermSize
-XX:MaxPermSize
-XX:MaxMetaspaceSize
The first sets the initial size of the Perm area, and the second sets the maximum value of the Perm area. In Java 8, the Perm area is removed and changed to Metaspace, but if you encounter a similar OOM, you can still resize it.
In addition, for applications that use a large number of threads, you can also configure-Xss, which is mainly used to set the stack size of a single thread. Note that it is a single size, so the higher the setting, the greater the footprint and the fewer threads available.
The configuration here generally starts with-X with a number plus a unit, while a-XX needs an equal sign followed by a number followed by a unit, for example:
Java-Xms100m-Xmx200m-XX:PermSize=300m
Here, the order after the number can be mmag, gjork, which represents different units in the computer.
Well, we have been talking about observing and analyzing the size of the heap and the size of each generation of the heap according to different applications. What is the specific observation?
We generally add some options to print GC logs in the configuration of JVM, which is similar to the above, so that when GC is generated, it will print out the size occupied by each generation, the specific trigger time, and so on. The following recommended configurations are recommended:
-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails
-Xloggc:
-XX:PrintGCDateStamps
You can choose either of the first and fourth options, the first printing the time since JVM was started, commonly known as uptime, and the fourth printing the current date and time of the system.
According to the content generated by the GC log, observe the specific size, and start to adjust using the above configuration parameters. Of course, you can also use JConsole, JVisual VM and other tools to visually understand and adjust. For the use of tools, you can refer to historical articles.
Java Seven weapon Series amorous Ring-- Multi-function Profiling tool JVisual VM
Garbage collection algorithm
Different garbage collection algorithms have a great impact on the application. On the one hand, a single-threaded recovery algorithm may be used on a server, or applications may have high requirements for response, but a throughput-first algorithm may be used, resulting in slow response.
Therefore, the choice of garbage collection algorithm is generally based on the characteristics of the application, whether to low latency or high throughput, choose the appropriate algorithm. As we mentioned earlier, the garbage collection algorithm and memory size configuration are not independent. If the memory setting is large, the frequency of collection will be reduced, but the execution time will be longer each time. So this is also a place to weigh.
Latency, throughput tuning
Other JVM configuration
Garbage collection algorithm corresponds to different garbage collectors, specific to the configuration in JVM, is to use different collectors such as-XX:+UseParallelOldGC or-XX:+UseConcMarkSweepGC to achieve the purpose of selecting algorithms.
ParallelGC, also known as throughput priority collector, can improve the throughput of applications, but in the old era, after several garbage collection, it can not meet the low latency requirements of applications.
ConcMarkSweepGC is commonly used, also known as CMS GC, which can achieve the old era of garbage collection and purebred parallel execution of applications, so it can achieve low latency.
Note here that because CMS GC and other GC recycling algorithms use different frameworks, they cannot be mixed. When using CMS for old-age recycling, the new generation uses single-thread recycling algorithm by default. In this case, you can configure-XX:+UseParNewGC to use new-generation parallel recycling.
Because CMS is parallel to garbage collection and application threads, additional CPU processing resources are required. If there is only one CPU machine or multiple busy CPU, and you want to use a low-latency collector, you can configure the incremental mode of the CMS collector to recycle, and turn on the incremental mode by specifying-XX:+CMSIncrementalMode. At this point, the garbage collector application thread runs alternately. Through configuration
-XX:CMSIncrementalSafetyFactor=X.-XX:CMSIncrementalDutyCycleMin=Y.
-XX:CMSIncrementalPacing can control how many CPU cycles are released by the garbage collection background thread for the application thread.
The parameter-XX:+CMSParallelRemarkEnabled is used to reduce the mark pause, and because the old memory space reclaimed by CMS is not continuous, the parameter is used.
-XX:+UseCMSCompactAtFullCollection 's compression of the older generation in Full GC.
The G1 collector is introduced in JDK1.7, which can be turned on by configuring-XX:+UseG1GC. There is not much actual combat experience in this area, friends who have relevant experience are welcome to share.
In addition, you can also make more detailed configurations for the new generation, such as setting the proportion of Eden and Suvivor areas, similar to Newxx, you can set the ratio through SuvivorRation.
Other JVM configuration
You can use the-XX:+DisableExplicitGC option to disable explicit calls to System.gc. This needs to be evaluated before use.
The so-called tuning is a process of continuous adjustment and optimization, which requires observation, configuration and testing. Friends with relevant experience are welcome to leave a message to add!
In the final analysis, where are the above options configured? We mentioned earlier that Tomcat is also an ordinary Java application in nature, so it is similar to the general Java startup mode.
Java-Xms100m-XX:+UseParallelOldGC application main class
To start in this form, the difference is that Tomcat puts the above commands into files, corresponding to different operating systems, bat files are used under Windows and sh files are used under Linux.
So our configuration items are also added to these files.
Let's take a look at the commands that are actually executed at startup in catalina.sh:
So our options can be added to
JAVA_OPTS
CATALINA_OPTS
One of these options.
Configuration is relatively simple, such as the following:
When configuring, you need to pay special attention to not flushing out the previous configuration, such as
When configuring JAVA_OPTS, add what has been configured earlier, and write it as follows:
JAVA_OPTS= "what's new in $JAVA_OPTS"
The above is how to tune the performance of JVM, 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.