Advanced 2: JVM startup parameters

Table of contents

jvm startup parameters

Parameter Classification

system property

Function Analysis

operating mode

The jvm has two modes of operation

heap memory

set heap memory

GC related

GC log related parameters

Analysis and diagnosis

Specify garbage collector related parameters

JavaAgent

What is a Java agent

common problem

video


foreword

There is no need to memorize too much in this course, just have an impression on yourself. Of course, it is better to know some commonly used ones.

jvm startup parameters

The startup parameters of the JVM (Java Virtual Machine) are some command-line parameters that can be set when starting the JVM. These parameters are used to specify the JVM's operating environment, memory allocation, garbage collector, and other options. The following are some common JVM startup parameters:

  1. -Xms: Set the initial heap size of the JVM.

  2. -Xmx: Set the maximum heap size of the JVM.

  3. -Xss: Set the stack size of each thread.

  4. -XX:MaxMetaspaceSize: Set the maximum size of the metaspace (replacing the permanent generation after JDK8).

  5. -XX:PermSize: Set the initial size of the permanent generation.

  6. -XX:MaxPermSize: Set the maximum size of the permanent generation (used before JDK8).

  7. -XX:NewSize: Set the initial size of the new generation.

  8. -XX:MaxNewSize: Set the maximum size of the new generation.

  9. -XX:SurvivorRatio: Set the ratio between the Eden area and the Survivor area.

  10. -XX:+UseParallelGC: Use a parallel garbage collector.

  11. -XX:+UseConcMarkSweepGC: Use concurrent mark-sweep garbage collector.

  12. -verbose:gc: Print GC log information.

  13. -Dproperty=value: Set system properties.

These startup parameters can be set by adding parameters when using the "java" command on the command line. For example: "java -Xms256m -Xmx512m -jar myapp.jar" will set the JVM's initial heap size to 256MB, maximum heap size to 512MB, and run the Java application named "myapp.jar".

idea use

Parameter Classification

1. Standard parameters starting with -, all JVMs must implement these parameters, and are backward compatible, such as -server. 2. -D Set system properties, such as -Dfile.encoding=UTF-8. 3. Non-standard parameters starting with -X are basically passed to the JVM. By default, the JVM implements the functions of these parameters, but it does not guarantee that all JVM implementations are satisfied, and there is no guarantee of backward compatibility. You can use the java -X command to view the non-standard parameters supported by the current JVM, such as -Xmx8g. 4. Unstable parameters starting with –XX: are specially used to control the behavior of the JVM, which is related to the specific JVM implementation and may be canceled in the next version at any time. -XX: ±Flags form, ± is to switch the Boolean value, such as -XX:+UseG1GC. -XX:key=value form, specify the value of an option, such as -XX:MaxPermSize=256m.

Common errors in the use of JVM startup parameters:

I believe that many developers have used java.lang.OutOfMemoryError. This is mainly caused by the JVM parameters not being configured properly. However, there are two types of errors: java.lang.OutOfMemoryError: Javaheapspace and java.lang.

OutOfMemoryError: PermGenspace, the former is the memory overflow related to heap memory, which can be set by configuring the -Xms and -Xmx parameters, and the latter is the memory overflow related to the permanent domain, which can be set by configuring -XX:MaxPermSize.

system property

I don't know about other languages, but as in java, no matter which company you go to, as long as you use a new computer. Work The first job is generally to configure system environment variables. For example, our jdk, etc., server deployment is the same.

Here we can use the System.getProperty("") method to configure, which is rare now. Just understand.

java -D configuration system property use cases In fact, we have been using -D parameter items unconsciously, for example, use the following parameters to configure file encoding: -Dfile.encoding=UTF-8

For another example, use the following parameters to configure dubbo options: java -Ddubbo.reference.com.foo.BarService.check=false java -Ddubbo.reference.check=false java -Ddubbo.consumer.check=false java -Ddubbo.registry .check=false

Function Analysis

-D=value official website explanation:

Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes: Set a property name/value pair in the system properties of a virtual machine, available to applications running on this virtual machine: System .getProperty("property name")

Get the value of value. If there is a space in the value, you need to enclose the value in double quotes, such as: -Dname="kazaf f". This parameter is usually used to set system-level global variable values, such as configuration file paths, to ensure that this property can be accessed anywhere in the program. Notes (1) JVM parameters need to be set instead of program parameters; (2) The parameters using this parameter have the highest priority and will override the configuration in the project;

operating mode

The jvm has two modes of operation

Client:

The default on 32-bit x86 machines prior to JDK1.7 was the -client option. Set the jvm to use the client mode, which is characterized by a relatively fast startup speed, but the runtime performance and memory management efficiency are not high, and it is usually used for client application or PC application development and debugging.

Server:

Set jvm to server mode, which is characterized by slow startup speed, but high runtime performance and memory management efficiency, suitable for production environments. This mode is enabled by default in a 64-bit capable jdk environment, ignoring the -client parameter.

There are two operating modes of the JVM, Server and Client. The difference between the two modes is that the startup speed of the Client mode is faster, and the startup speed of the Server mode is slower; but after the startup enters a stable period and runs for a long time, the running speed of the program in the Server mode is much faster than that of the Client. This is because the JVM started in the Server mode uses a heavyweight virtual machine, which uses more optimizations for the program; while the JVM started in the Client mode uses a lightweight virtual machine. So the Server starts slowly, but the speed is much faster than the Client after stabilization.

-Xint:

In interpreted mode, the -Xint flag forces the JVM to interpret all bytecode, which of course slows things down, usually by a factor of 10 or more.

-Xcomp:

The -Xcomp parameter is just the opposite of -Xint. The JVM will compile all bytecodes into native code when it is used for the first time, thereby bringing the greatest degree of optimization.

-Xmixed:

-Xmixed is a mixed mode, which mixes the interpretation mode and the mutation mode, which is decided by the JVM itself. This is the default mode of the JVM and is also a recommended mode. We can see mixed mode and other information by using java -version.

heap memory

JVM total memory = heap + stack + non-heap + off-heap memory

set heap memory

The memory setting of the JVM is the most important parameter setting, and it is also the focus of GC analysis and tuning. JVM total memory = heap + stack + non-heap + off-heap memory.

-Xmx:

Specifies the maximum heap memory. Such as -Xmx4g. This only limits the maximum value of the Heap part to 4g. This memory does not include stack memory, nor does it include memory used outside the heap.

-Xms:

Specifies the initial size of the heap memory space. Such as -Xms4g. Moreover, the specified memory size is not the initial value actually allocated by the operating system, but the GC plans it first and then allocates it when it is used. The dedicated server needs to keep -Xms and -Xmx consistent, otherwise there may be several FullGCs just after the application starts. When the two configurations are inconsistent, heap memory expansion may cause performance jitter.

-Xmn:

Equivalent to -XX:NewSize, this option should not be set when using the G1 garbage collector, but can be set in some other business scenarios. The official recommendation is to set it to 1/2 ~ 1/4 of -Xmx.

-XX:MaxPermSize=size:

This was used before JDK1.7. The Meta space allowed by Java8 is infinite by default, so this parameter is invalid.

-XX:MaxMetaspaceSize=size:

Java8 does not limit the Meta space by default, and it is generally not allowed to set this option.

XX:MaxDirectMemorySize=size:

The maximum off-heap memory that the system can use, this parameter has the same effect as -Dsun.nio.MaxDirectMemorySize.

-Xss:

Sets the number of bytes per thread stack. For example -Xss1m specifies that the thread stack is 1MB, which is equivalent to -XX:ThreadStackSize=1m

Notice:

Off-heap memory: That is to say, memory that is not on the heap, we can view it through jconsole, jvisualvm and other tools. For both IBM JVM and Hotspot, the maximum value of the nio direct buffer can be set by -XX:MaxDirectMemorySize. The default is 64M. When it exceeds this value, it will automatically increase according to 32M.

There are a large number of places in the system that use off-heap memory, which is much wider than what we often say about xmx and xms. So we need to leave room when setting the memory. Generally speaking, it is best to configure 60-80% of the available memory in the system or container.

GC related

​-XX:+UseG1GC: Use the G1 garbage collector

-XX:+UseConcMarkSweepGC: use CMS garbage collector

-XX: +UseSerialGC: Use the serial garbage collector

-XX: +UseParallelGC: use parallel garbage collector

/ Java 11+

XX: +UnlockExperimentalVMOptions -XX:+UseZGO/ Java 12+

-XX: +UnlockExperimentalVMOptions -XX:+UseShenandoahGO

GC log related parameters

In the production environment or performance stress testing environment, one of the important data sources we use to analyze and judge problems is the GC log. The JVM startup parameters provide us with some options for controlling the output of the GC log.

-verbose:gc :

Used in combination with other GC parameters to output detailed GC information in the GC log. Including the size of each memory pool before and after each GC, the size of the heap memory, the size of promotion to the old generation, and the time consumed. This parameter supports dynamic switching during operation. Such as using jcmd, jinfo, and other clients using JMX technology.

-XX:+PrintGCDetails 和 -XX:+PrintGCTimeStamps:

Print GC details and occurrence time.

-Xloggc:file:

Similar to the -verbose:gc function, it just records the relevant information of each GC event into a file, and the location of the file is preferably local to avoid potential problems with the network. If it appears in the command line together with the verbose:gc command, -Xloggc shall prevail.

Analysis and diagnosis

Specify garbage collector related parameters

The garbage collector is one of the core contents of JVM performance analysis and tuning, and it is also the place where recent JDK versions have vigorously developed and improved. Through different GC algorithms and parameter combinations, together with other tuning methods, we can accurately verify the system to the best performance state.

The following parameters specify specific garbage collectors.

-XX:+UseG1GC: Use the G1 garbage collector

-XX:+UseConcMarkSweepGC: use the CMS garbage collector

-XX:+UseSerialGC: use the serial garbage collector

-XX:+UseParallelGC: use parallel garbage collector

JavaAgent

Agent is a black technology in JVM. It can do many things in a non-invasive way, such as injecting AOP code, executing statistics, etc., with very large permissions. Here is a brief introduction to the configuration options, and the detailed functions need to be discussed specifically.

The syntax for setting an agent is as follows:

-agentlib:libname[=options] Enable the agent in native mode, refer to the LD_LIBRARY_PATH path

-age-ntpath:pathname[=options] Enable the agent in native mode.

-javaagent:jarpath[=options] Enable external agent libraries, such as pinpoint.jar, etc.

-Xnoagent disables all agents

The following example enables CPU usage time sampling analysis:

JAVA _OPTS="-agentlib:hprof=cpu=samples,file=cpu.samples.log"

What is a Java agent

Java agent is a technology introduced since JDK1.5, which supports running jar attached to the JVM process.

Java agent is divided into static loading and dynamic loading two ways of agent loading, as shown in the figure below:

Reference document: https://blog.csdn.net/BASK2311/article/details/127491913

common problem

1. Since some non-standard parameters of jvm cannot be supported, how do we check whether the parameters we want to use are effectively supported?

The answer is: java -x

2. I also encountered what was mentioned in the course video when I was working before, that is, we have no user volume for a service name, but there are always ooms. After we checked the GC log, there was no obvious problem. At this time, I need to look at the startup parameters.

The maximum memory value of our -xmx configuration must not exceed 80% of the local server, and xms is also the same as xmx.

Otherwise there is no way to leave room for some non-heap memory and other programs. Under normal circumstances, 60-80% can be directly configured, and now the server generally starts with 1G.

3. What is the default garbage collector for each major version of Java? What about java8? We will talk about this later in the GC algorithm, and those who are interested can take a look at it first, and then we can discuss it together.

video

Link: https://www.aliyundrive.com/s/bfS1cHS9z8z

Let's stop here today, friends who feel useful can give a thumbs up, your support is the biggest motivation for me to update!

Guess you like

Origin blog.csdn.net/zth_killer/article/details/131438110