Java Startup Params

Basic Format

Run java -help to see the basic format and options

[root@localhost ~]# java -help
Usage: java [-options] class [args...]
           (execute class)
   or java [-options] -jar jarfile [args...]
           (execute jar file)
Startup options are omitted
[-options]: options
[-options]: options [args...] : Passes a parameter to the main method

Standard options

  • -help / -? : output help information
  • -version : output version information
  • -classpath / -cp: class search path for directories and zip/jar files
  • ... Other uncommon parameters

Non-standard options (the X parameter)

Compile mode options.

  • -Xint : interpreted mode execution only
  • -Xcomp: opposite of -Xint, fully compile on first use
  • -Xmixed : (default) mixed mode

Examples of the three modes of JVM operation.

Example of operation mode

Abbreviated options for advanced options (xx parameters)

  • -Xms: Sets the minimum and initial size of the heap. This parameter is equal to -XX:InitialHeapSize
  • -Xmx: Sets the maximum size of the heap, -Xms is usually set to the same value as -Xmx. This parameter is equal to -XX:MaxHeapSize
  • -Xmn : Sets the initial and maximum size of the young Generation heap. This parameter is equal to setting both -XX:NewSize and -XX:MaxNewSize.
  • -Xss: Sets the individual thread stack size, typically 512k ~ 1024k. This parameter is equal to the same as -XX:ThreadStackSize. The default values are as follows: (depends on the platform)
    • Linux/ARM (32-bit): 320 KB
    • Linux/i386 (32-bit): 320 KB
    • Linux/x64 (64-bit): 1024 KB
    • OS X (64-bit): 1024 KB
    • Oracle Solaris/i386 (32-bit): 320 KB
    • Oracle Solaris/x64 (64-bit): 1024 KB
    • Windows: determined by virtual memory

Advanced options (XX parameters)

Classification methods

According to the official classification, there are four types as follows

  1. Advanced Runtime Options
  2. Advanced JIT Compiler Options
  3. Advanced Serviceability Options
  4. Advanced Garbage Collection Option

According to the classification of the way to set parameters, there are two types as follows

  1. Boolean type.
  • -XX:+ means on
  • -XX:- means off
  1. KV set value type: -XX:Key=Value
  • Capacity type (no B suffix is allowed)
    • k or K for KB
    • m or M for MB
    • g or G for GB
    • Numeric unitless representation B
  • Numeric
    • Size
    • Proportional

Common settings

  • -XX:+PrintCommandLineFlags : Enables printing of JVM configuration on the command line, such as heap space size and the selected garbage collector. Disabled by default.
  • -XX:MetaspaceSize=size : Sets the size of the metadata space that will trigger garbage collection when it is exceeded for the first time. This threshold for garbage collection increases or decreases depending on the amount of metadata in use. The default size depends on the platform.
  • -XX:MaxMetaspaceSize=size : Sets the maximum amount of native memory for metadata. By default, there is no limit on the size. The amount of application metadata depends on the application itself, other running applications, and the amount of memory available in the system.
  • -XX:SurvivorRatio=ratio: Sets the ratio between the size of Eden space and the size of Survivor space. The default value is 8, i.e. Eden:S0:S1 = 8:1:1

Heap Setup Lifecycle

  • -Xms -Xmx : Usually set to the same value
  • -Xmx : Young Generation heap region is used for new objects. This area performs GC more often than other areas. If the young generation is too small, then Young GC will be executed frequently. If it is too large, then only Full GC will be executed, which may take a long time to complete. Oracle recommends keeping the young generation heap size between half and a quarter of the overall heap size
  • -Xss: generally set to 1M
  • -XX:+PrintCommandLineFlags : recommended to enable
  • -XX:MetaspaceSize=size -XX:MaxMetaspaceSize=size : Generally 256M is sufficient
  • -XX:SurvivorRatio=ratio : default value is 8, recommended to add

It is recommended to lower the value of the above startup parameters when the virtual machine is running low on resources,for example.

java -XX:+PrintCommandLineFlags -Xms128M -Xmx128M -Xmn64M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -XX:SurvivorRatio=8 -jar web.jar