To learn about JVM tuning, we need to know some of their parameters in advance so that we can use them better

Common command-line arguments for the JVM

The JVM command-line parameters reference: docs.oracle.com/javase/8/do…

1. View the parameter list

Vm parameters are classified into basic and extended parameters. You can enter JAVA_HOME\bin\ Java on the cli to obtain the basic parameter list. To get the extended parameter list, type JAVA_HOME\bin\ Java -x on the command line.

2. Basic parameter Description:

– Client, -server: two Java VM startup modes. The Client mode is faster but has poor performance and memory management. The Server mode is slower but has high performance

-classpath -cp: specifies the mode in which the VM searches for a class when it runs in the memory: Bootstrap classes, Extension classes, and User classes. The Bootstrap paths are jar or ZIP files provided by the VM. The VM searches for these package files first and uses system.getProperty (“sun.boot.class.path”) to obtain the package name. Extension is a JAR file in the jre\lib\ext directory that the virtual machine searches for after Bootstrap. Use System.getProperty (“java.ext.dirs “) to get the virtual machine to use

Extension Search path. The search order for User classes is the current directory, CLASSPATH, and -classpath.

-classpath: tells the VM to search for the directory name, JAR document name, and ZIP document name

Verbose [: class | gc | jni] : Verbose displays vm running information on the output device. -verbose:class Displays information about classes mounted on the VM. -verbose: GC Displays device information when memory reclamation occurs. -verbose: displays configuration information when the JNI VM invokes the native method, which is used to monitor the invocation of the local method

-version: displays the version information about running VMS

-showversion: displays the version and help information

3. Extended parameter description:

-xmixed: Indicates that the -client mode is used to compile and execute just-in-time for frequently used methods, and the other methods are executed In explain mode, which is the default mode of the VM

-xint: Enables VMS running in -client mode to execute bytecodes of classes in interpretive mode and do not compile bytecodes to local codes, which may cause performance loss

-xbootCLASSPath :path, -xbootclasSPath /a:path, -xbootclasspath /p:path: Changed the virtual machine to load the system run package rt.jar and load the system run classes from the search path set in -xbootCLASspath. You won’t use this parameter unless you can write a runtime yourself. /a: The search path in path is added to the default search path. /p: Searches the path before the default path.

-Xnoclassgc: disables the garbage collection function for class on the VM, which may cause OutofMemoryErrors

-Xincgc: the incremental garbage collector is enabled. The incremental garbage collector is disabled by default. The incremental garbage collector can reduce the pause time caused by the occasional long garbage collection

-xloggc :file: writes the garbage collection information of each VM to a log file. The file name is specified by file. The file format is a flat file, and the content is the same as that of -verbose:gc

-xms20m: specifies the initial size of the available memory heap for the vm to be 20 MB (an integer multiple of 1024) and greater than 1MB. The maximum memory size can be set in the unit of K or M. The initial heap size is 2MB, for example, -xms256m

-xmx20m: specifies the maximum available size of the vm memory heap. The value must be an integer multiple of 1024 and greater than 2MB. A larger memory size can be set in units of K or M, for example: – Xmx81920K, – Xmx80M, applied for large memory when the application runtime throws a Java virtual machine. The lang. OutOfMemoryError, -xmx is required to set up

-xss128K: Sets the size of the thread stack. Similar to -xmx, you can use K or M to set larger values. You can also 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 virtual machine stacks and local method stacks, so this parameter is invalid for HotSpot

-xx :PermSize=10M: Indicates the capacity of the permanent generation initially allocated by the JVM. The unit must be M

-xx :MaxPermSize=10M: Indicates the maximum capacity of permanent generations that the JVM can allocate. This parameter must be expressed in M. In most cases, this parameter defaults to 64 MB

-xx :NewRatio=4: Sets the young generation: the size ratio of the old generation is 1:4, which means that the young generation occupies 1/5 of the heap

-xx :SurvivorRatio=8: indicates that two Survivor zones are set. The size ratio of one Eden zone is 2:8, which means that Survivor zones account for 1/5 of the entire young generation. The default parameter is 8

-Xmn20M: sets the size of the young generation to 20M

– XX: + HeapDumpOnOutOfMemoryError: offered virtual machine when abnormal memory Dump out the current heap memory Dump snapshot

-xx :+UseG1GC: tells the JVM to use the G1 garbage collector

-xx :+PrintGCDetails: Prints GC details on the console

-xx :+PrintGC: Prints GC information on the console

– XX: PretenureSizeThreshold = 3145728 said objects larger than 3145728 (3 m) straight into the old s allotment, here only in bytes as a unit

-xx :MaxTenuringThreshold=1: indicates that the age of the object is greater than 1

Vm parameter classification

Standard: -, all HotSpot support non-standard: -x, certain versions of HotSpot support certain command unstable: -xx, may be cancelled in the next version

For example, java-version and 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[1024*1024]; list.add(b); }}}Copy the code

Distinguishing concepts: Memory leak, 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 value Java -xx :+PrintFlagsFinal Final parameter value Java – XX: + PrintFlagsFinal | grep XXX to find the parameters of the corresponding Java – XX: + PrintFlagsFinal – version | grep GC

Basic concepts before tuning

Throughput: user code time/(user code execution time + garbage collection time) 2. Response time: The shorter The STW (Stop The World), The better The response time

So-called tuning, to clear yourself, want what, throughput or response time, or in the case of satisfy certain response time, required much throughput, in general, according to the type of operation to choose corresponding tuning methods, such as web site response time is need a priority, JDK1.8 pick G1 as far as possible, So if it’s data mining you need to use throughput.

What is tuning

JVM tuning, as we understood it before we touched on it, is about solving OOM, and OOM is just a part of JVM tuning

  • The JVM is typically planned and pre-tuned as required
  • Optimizing the running JVM runtime environment (slow, sluggish)
  • (OOM) Resolve various JVM runtime issues (OOM)

First of all, tuning begins the business scenario, without the business scenario the JVM tuning is unreliable, such as have the time in the actual project, there are a lot of classes, tens of thousands of code, how do you know which is the specific code has a problem, even if we know that there are code frequently full gc, but may OOM up over a period of time.

Tuning steps:

  • Familiarize yourself with the business scenario and select the garbage collector (there is no best garbage collector, only the most appropriate garbage collector)

    1. Response time, pause time [CMS G1 ZGC] (need to respond to user)
    2. Throughput = User time /(User time + GC time) [PS]
  • Select the collector combination

  • Calculate memory requirements (Set the memory size to 1.5 GB and 16 GB)

  • Select CPU: The higher the better

  • Set age and upgrade age

  • Setting Log Parameters

    1. -Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCCause
    2. Or one log file per day

In the production environment, log files with the following log names are generated according to the system time and generated in a cycle. There are five log files with a size of 20M each. The advantage of this is that the overall size of the log files is 100M and the overall file size can be controlled

  • Observing logs

Tuning case

Case a

Vertical e-commerce, maximum daily million orders, what kind of server configuration is required for order processing system

This problem is trivial, as many different server configurations can support it

Such as the one million orders, each hour won’t produce high concurrency, we are looking for the rush hour, make an assumption in order for 100 w, 72 w order peak, such as an hour on average 36 w order, so we choose the memory size is in accordance with the peak time of choice, many time we may be doing pressure test, If you have to, add CPU and memory

Case 2

12306 suffered a massive tickets during the Spring Festival should be how to support the order information is fixed, every day can throw to the cache, the business logic of the different design have different business, 12306 should be is one of the biggest Chinese concurrency seconds kill website, so-called concurrent 100 w, that is to say one million times per second the business logic of the process, estimate taobao highest is 54 w concurrent in a year.

To solve this problem, look at the following:

CDN – LVS – NGINX – Business System – 1W concurrency per machine

The ordering process of ordinary e-commerce is as follows:

TPS will not last long if the transaction is completed synchronously

But in 12306, the model is to place an order – “inventory reduction and order at the same time asynchronously -” waiting for payment asynchronous is when you place an order, it is a thread to reduce inventory, the other thread directly throw your order information into Kafka or Redis directly return OK, after you place a successful order waiting for you to pay, When you finish the payment, the order processing threads will go to get the data, and the processing will be persisted to Hbase or mysql. The core idea of the general processing method of large traffic is: divide and conquer

The JVM to optimize

For example, I have a 500,000-PV data website (extracting documents from disk to memory), the original server 32 bits, 1.5G heap, user feedback is relatively slow, if it is upgraded, the new server 64 bits, 16G heap memory, users are still stuck, and more serious than before, what is the reason? Generally speaking, a lot of users browse data, a lot of data will load into the 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.

The other thing is that the system CPU is always 100%. How do we tune it?

The first thing we can think of is that CPU100% means that there must be threads occupying system resources

  1. Find which process has the highest CPU (top command)
  2. Which thread in the process has the highest CPU (top-HP)
  3. Export the stack for that thread
  4. Finding out which method (stack earn) is more time-consuming
  5. Worker threads of high | gc thread high proportion

conclusion

Today we just explained some basic operations, specific how to operate how to do? This part of small farmers will explain in the next part, today mainly take you to understand some commonly used parameters, tell you how to use and some pre-knowledge, I will do a practical explanation of these problems, interested partners remember to one button three, thank you.

I am a small farmer, afraid of what truth is infinite, further have further joy, everyone refueling!!