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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how the Java virtual machine is tuned. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
JVM common command line parameter 1. View parameter list
Virtual machine parameters are divided into basic and extended types. Enter JAVA_HOMEinjava on the command line to get a list of basic parameters.
Type JAVA_HOMEinjava-X on the command line to get a list of extended parameters.
two。 Basic parameters description:
-client,-server: two Java virtual machine startup modes: client mode starts relatively fast, but performance and memory management are relatively poor. Server mode starts slowly, but the running performance is relatively high. Client mode is used on windos, and server mode is used in Linux mode.
-classpath-cp: when a virtual machine runs a class, it needs to be transferred to memory. The way and order in which the virtual machine searches for classes: Bootstrap classes, Extension classes, User classes.
The path in Bootstrap is the jar or zip file that comes with the virtual machine. The virtual machine searches these package files first, and uses System.getProperty ("sun.boot.class.path") to get the package name searched by the virtual machine.
Extension is a jar file located in the jrelibext directory, and the virtual machine searches for the jar file in this directory after searching for Bootstrap. Use System. GetProperty ("java.ext.dirs") can be used by virtual machines
Extension search path.
The User classes search order is the current directory, environment variables CLASSPATH,-classpath.
-classpath: tell the virtual machine to search directory name, jar document name, zip document name
-verbose [: class | gc | jni]: displays virtual machine operation information on the output device
-verbose:class outputs information about classes loaded by the virtual machine
-verbose:gc outputs device information when memory collection occurs on a virtual machine, which is used to monitor memory recovery of a virtual machine.
-output setting display information when the verbose:jni virtual machine calls the native method, which is used to monitor the virtual machine calling the local method
-version: displays runnable virtual machine version information
-showversion: displays version information and help information
3. Extension parameter description:
-Xmixed: set-client mode virtual machine compiles and executes Just-In-Time for frequently used methods, and interprets other methods, which is the default mode for virtual machines
-Xint: the virtual machine running in the setting-client mode executes the bytecode of the class in an interpretive manner, without compiling the bytecode to native code, which may result in performance loss.
-Xbootclasspath:path,-Xbootclasspath/a:path,-Xbootclasspath/p:path: change the virtual machine mount system to run the package rt.jar and load the system run classes from the search path set in-Xbootclasspath. This parameter will not be used unless you can write a runtime yourself.
/ a: the search path in path will be added to the default search path.
/ p: search the search path in path before the default search path.
-Xnoclassgc: turn off the garbage collection function of the virtual machine to class, which may cause OutOfMemoryError
-Xincgc: start the incremental garbage collector, which is off by default. The incremental garbage collector can reduce the pause time caused by occasional long-term garbage collection, but the concurrent execution of the incremental garbage collector and the application will occupy some of the functions of CPU on the application.
-Xloggc:file: writes the information of each garbage collection of the virtual machine to the log file. The file name is specified by file. The file format is a flat file, and the content is the same as the output of-verbose:gc.
-Xms20M: set the initial size of the available memory heap of the virtual machine to 20m, an integer multiple of 1024 and greater than 1MB. You can use K or M to set a larger number of memory, and the initial heap size is 2MB, for example:-Xms256M
-Xmx20M: set the maximum available size of the virtual machine memory heap, which must be an integer multiple of 1024 and greater than 2MB. You can set a large amount of memory in K or M units, for example:-Xmx81920K,-Xmx80M. If the virtual machine throws java.lang.OutOfMemoryError when the application requests a large memory to run, you need to use-Xmx to set it.
-Xss128K: sets the size of the thread stack, similar to-Xmx. You can use K or M to set a larger value, or you can set the stack size when creating thread objects in Java.
-Xoss128k: sets the size of the local method stack to 128K, but HotSpot does not distinguish between the virtual machine stack and the local method stack, so this parameter is not valid for HotSpot
-XX:PermSize=10M: indicates the capacity of the permanent generation initially allocated by JVM, which must be in M
-XX:MaxPermSize=10M: indicates the maximum capacity of the permanent generation allowed by JVM, which must be in M. In most cases, this parameter defaults to 64m.
-XX:NewRatio=4: sets the younger generation: the size ratio of the old generation is 1:4, which means that the younger generation accounts for 1 prime 5 of the whole heap.
-XX:SurvivorRatio=8: sets 2 Survivor zones: the size ratio of 1 Eden zone is 2:8, which means that the Survivor area accounts for 1 Survivor 5 of the whole younger generation. This parameter defaults to 8.
-Xmn20M: set the size of the younger generation to 20m
-XX:+HeapDumpOnOutOfMemoryError: allows the virtual machine to Dump the current heap memory dump snapshot when a memory overflow exception occurs
-XX:+UseG1GC: indicates that JVM uses the G1 garbage collector
-XX:+PrintGCDetails: prints out GC details on the console
-XX:+PrintGC: GC information is printed on the console
-XX:PretenureSizeThreshold=3145728 means that if the object is greater than 3145728 (3m), it can be allocated directly to the old age. Here, it can only be in bytes.
-XX:MaxTenuringThreshold=1: indicates that the object is older than 1 and automatically enters the old age
Virtual machine parameter classification
Standard:-at the beginning, all HotSpot supports
Non-standard: starting with-X, specific versions of HotSpot support specific commands
Unstable:-XX start, the next version may be canceled
For example: java-version, java-X
Import java.util.List;import java.util.LinkedList;public class HelloGC {public static void main (String [] args) {System.out.println ("HelloGC!"); List list = new LinkedList (); for (;;) {byte [] b = new byte [1024x1024]; list.add (b);}
Distinguishing concepts: memory leak memory leak, memory overflow out of memory
Java-XX:+PrintCommandLineFlags HelloGC
Java-Xmn10M-Xms40M-Xmx60M-XX:+PrintCommandLineFlags-XX:+PrintGC HelloGC
PrintGCDetails PrintGCTimeStamps PrintGCCauses
Java-XX:+UseConcMarkSweepGC-XX:+PrintCommandLineFlags HelloGC
Java-XX:+PrintFlagsInitial default parameter valu
Java-XX:+PrintFlagsFinal final parameter value
Java-XX:+PrintFlagsFinal | grep xxx finds the corresponding parameter
Java-XX:+PrintFlagsFinal-version | grep GC
Basic concepts before tuning
1. Throughput: user code time / (user code execution time + garbage collection time)
two。 Response time: the shorter the STW (Stop The World), the better the response time
For the so-called tuning, first of all, you should be clear about what you want, throughput or response time, or how much throughput is required to meet a certain response time. Generally speaking, choose the corresponding tuning method according to the type of business. For example, if the website needs response time first, JDK1.8 chooses G1 as far as possible, then throughput is used for data mining.
What is tuning?
Before we come into contact with tuning, we understand that JVM tuning is to solve the OOM problem, and OOM is only part of JVM tuning.
Generally, according to the demand, JVM planning and pre-tuning to optimize the operation of JVM operating environment (slow, stutter) to solve various problems in the process of JVM operation (OOM)
First of all, tuning starts with business scenarios. If there is no JVM tuning in business scenarios, for example, there are many classes and thousands of codes in a real project, how do you know which code has a problem? even if we know that there is a frequent full gc of code, it may become OOM after a period of time.
1. Tuning steps:
Familiar with the business scenario, select the garbage collector (there is no best garbage collector, only the most suitable garbage collector)
Response time, pause time [CMS G1 ZGC] (need to respond to users) Throughput = user time / (user time + GC time) [PS]
Select recycler combination
Calculate memory requirements (set memory size 1.5G 16G)
Select CPU: the higher the better
Set the age and upgrade age
Set log parameters
-Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log-XX:+UseGCLogFileRotation-XX:NumberOfGCLogFiles=5-XX:GCLogFileSize=20M-XX:+PrintGCDetails-XX:+PrintGCDateStamps-XX:+PrintGCCause or generate a log file every day
In the production environment, the log file, followed by the log name, is generated according to the system time, the cycle is generated, and the number of logs is five, each with a size of 20m. This advantage is that the overall size is 100m and the overall file size can be controlled.
Observe the log
two。 Tuning case 2.1 case 1
Vertical e-commerce, up to millions of orders per day, what kind of server configuration is required for the order processing system
This problem is more chicken, because many different server configurations can support it.
For example, if there are a million orders that do not produce very high concurrency per hour, we look for peak hours and make a hypothesis that 72W orders for 100W orders are generated during peak hours, such as an average of 36W orders per hour, so our memory selection size is chosen according to the peak time. A lot of time we may just go to do stress tests, but if we really can't, just add CPU and memory.
2.2 case two
How to support the massive ticket grabbing during the Spring Festival in 12306?
Order information is fixed every day and can be thrown into the cache. Different business logic has different business designs. 12306 should be the website with the largest concurrency in China, known as concurrency 100W, that is, millions of business logic processing per second. It is estimated that Taobao has the highest concurrency of 54W in a year.
If you solve this problem, see the following:
CDN-"LVS -" NGINX-"Business system -" 1W concurrency per machine
The order issuing process of ordinary e-commerce is generally as follows:
Order-"place order -" order system reduce inventory-"waiting for user to pay
If this transaction is completed synchronously, TPS will not last long.
But the model in 12306 is
Place order-"reduce inventory and order asynchronously at the same time -" waiting for payment
Asynchronism means that when you place an order, one thread reduces inventory, and the other thread directly throws the information about your order into kafka or redis and returns OK directly. After you successfully place an order, wait for your payment. When your payment is completed, those order processing threads will go inside to get the data. After this processing is finished, it will persist to Hbase or mysql. The core idea of the general processing method of large traffic is: divide and conquer.
JVM optimization
For example, I have a 500000 PV data site (extracting documents from disk to memory) 32-bit original server, 1.5G heap, user feedback site is relatively slow, if you upgrade it, the new server 64-bit, 16G heap memory, users still feedback stutter, and it is more serious than before, what is the reason for this? Generally speaking, many users browse data, a lot of data will be load to memory, resulting in insufficient memory, frequent GC,STW time is too long, the response time will be slow, then what should we do, use PS- > PN+CMS or G1.
Another is that the system CPU is often 100%. How can we tune it?
First of all, we can think of CPU100%, so there must be threads taking up system resources.
1. Find out which process has higher CPU (top command)
two。 Which thread in this process has a high CPU (top-Hp)
3. Export the stack of this thread
4. Find out which method (stack earning) is more time-consuming
5. High proportion of worker threads | High proportion of garbage collection threads
Thank you for reading! This is the end of the article on "how to tune the Java virtual machine". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.