What are JVM parameters

In the SLR, there are several modes, P/A/S/M. Among them, P is the dumb mode. The program will automatically set the shutter speed and aperture size according to the environment, so as to get A relatively appropriate exposure effect. A mode is aperture priority. The user can set the aperture size and shutter speed by the camera program, which is similar to the semi-automatic mode. The S-mode is shutter priority mode, similar to the A-mode, except that the user can set the shutter speed. The last mode is M mode, which is purely manual mode. Users can adjust the shutter speed, aperture size, etc., which requires a lot of people, but many experts often choose M mode to shoot their own works.

Think of the JVM as a camera, and the JVM parameters are parameters like aperture size and shutter speed that have a big impact on how your program works.

Java programs run on the JVM, and the JVM automatically sets some JVM parameters based on the environment, but these parameters are not guaranteed to be optimal. Some parameters are basically set at startup and cannot be adjusted at runtime. In order to let the JVM can better running your program, or it is necessary for the JVM parameters have certain understanding, to know what the JVM parameter respectively in these scenarios, have what effect, such as phase we don’t expect classes can be unloaded, whether can print some log at run time to help us to understand the operation situation of the JVM If something goes wrong, can you automatically hold some of the field data for us? These can be set by JVM parameters.

How many JVM parameters are there

You’d be surprised how many JVM parameters there are. There are about 1000 or more. You can go to the JVM parameter | PerfMa application performance technology community to look at all these JVM parameters (note: This is the PerfMa community’s forum to share experiences with JVM parameters. Of course, you don’t have to know every JVM parameter, but there are some common JVM parameters that are useful for performance tuning.

Where JVM parameters are normally set

It’s easy to start a Java program with commands like the following

java Main
Copy the code

As we all know, Main is the program’s startup class. JVM execution will find the following signed functions in this Main class

Public static void main(String args[])
Copy the code

So how do you pass in the parameter args to this function? We do this by adding relevant parameters to the main class of the launch command, separated by Spaces, that the JVM automatically passes in as part of the ARGS, for example

java Main arg1 arg2
Copy the code

Arg1 and arg2 are automatically filled into the args array so that you can use them in your program

Arg1 and arg2 are called program arguments, but the JVM arguments are not program arguments, they are JVM arguments. Where do WE put JVM arguments? JVM arguments are placed before the main class and after Java commands, such as

java -Xmx100M Main arg1 arg2
Copy the code

-xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m = -xmx100m Of course, there may be other reasons why the JVM parameters may differ from what you set

How to write JVM parameters

How to write JVM parameters

  • -x, for example, -xmx100m
  • “-xx:”, such as -xx :+UseG1GC
  • For example, -verbose: GC

-x and – are usually converted to one or more -xx: arguments, just a simplified version, such as -xmx100m, which is automatically converted to -xx :MaxHeapSize=100M. -verbose:class automatically converts to -xx :+TraceClassLoading -xx :+TraceClassLoading

Specify the JVM parameter file with the Flags parameter

If JVM parameters are shipped with the source code, it’s not very friendly to have to pull a branch and commit code just to change JVM parameters. What’s the best way to do that?

We can set a parameter in the startup parameter, which is similar to the following

java -XX:Flags=/home/admin/flags Main arg1 arg2
Copy the code

After setting this parameter, we simply create a flags file in the /home/admin directory of the service and specify all JVM parameters in this file. However, there are some requirements for the flags file to be written. For example, -xx :MaxHeapSize=100M should be replaced by -xx :MaxHeapSize=100M. Do not use -xx: as long as key=value or +/-key.

MaxHeapSize=8G +UseG1GC
Copy the code

It’s equivalent to

-Xmx8G -XX:+UseG1GC
Copy the code

This can be verified by adding -xx :+PrintVMOptions to print the JVM parameters that have been set, for example

java -XX:Flags=/home/admin/flags -XX:+PrintVMOptions Main arg1 arg2
Copy the code

The JVM parameter file is specified with the VMOptionsFile parameter

If your JDK version is larger than 1.8, the JVM provides a more personalized option to use VMOptionsFile instead of Flags, which also specifies a file. The JVM parameters in this file are written exactly the same as the JVM parameters we wrote after the Java command

java -XX:VMOptionsFile=/home/admin/flags Main arg1 arg2
Copy the code

So in the FLAGS file we can do this

-Xmx8G -XX:+UseG1GC
Copy the code

Isn’t that a lot easier

Start learning about JVM parameters

We also open a free course to learn JVM parameters in the community. If you are interested, please come to PerfMa community to learn about them. We also welcome you to participate in the discussion and gradually uncover the mystery of JVM parameters.

JVM parameters from PerfMa KO series