Java virtual machine-3-JVM parameters

Three, JVM parameters

1. Standard options

There are 2 commonly used:

-server         选择 "server" VM
                默认 VM 是 server.

-D<名称>=<>    设置系统属性

64-bit JDK, Java HotSpot™ 64-Bit Server VM, the default is the server option

2. Non-standard options

java -XSet by command

common:

-Xms<size>    设置初始 Java 堆大小
-Xmx<size>    设置最大 Java 堆大小
-Xss<size>    设置 Java 线程堆栈大小
-Xmn<size>    设置新生代大小

Size: 1g=1024m=1048576k=1073741824

Note: If -Xss (Java thread stack size) is set incorrectly, java.lang.OutOfMemoryError: unable to create new native threadan exception will be thrown

(MaxProcessMemory - JVMMemory - ReservedOSMemory) / ThreadStackSize

Number of threads = (maximum memory of a process-JVM memory-reserved operating system memory) / thread stack size

That is to say, the number of threads that the system can create is inversely proportional to the memory size allocated to the JVM

Default size:

  • Linux/x64 (64-bit): 1024 KB
  • OS X (64-bit): 1024 KB
  • Windows: Depends on virtual memory

If you use the CMS garbage collector, the recommended configuration is

(Xmx - Xmn) * (100 - CMSInitiatingOccupancyFraction) / 100 >= Xmn

3. Options that are unstable and not recommended for casual use

[1] Option rules

There are two main java -XX:参数名=参数值and java -XX:+参数名[open], java -XX:-参数名[close]

[2] View the default value

Use jinfocommands to view information about the specified Java process

jinfo -flag 参数名 进程ID    查看指定参数的信息
jinfo -flags 进程ID         查看此Java进程的所有参数设置情况

View the initial configuration and the modified configuration

java -XX:+PrintFlagsInitial    查看出厂配置
java -XX:+PrintFlagsFinal      查看修改后的配置,如果有调整,则会在等号前面添加一个冒号(:=

View the parameters entered when the program starts

-XX:+PrintCommandLineFlags

[3] Print garbage collection details log

-XX:-XX:+PrintGCDetails

Take JDK8 as an example

-Xms1m -Xmx1m -XX:+PrintGCDetails
public static void main(String[] args) {
    
    
    byte[] arr = new byte[1024 * 1024];
}
[GC (Allocation Failure) [PSYoungGen: 512K->488K(1024K)] 512K->496K(1536K), 0.0006315 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 994K->488K(1024K)] 1002K->632K(1536K), 0.0024668 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 991K->488K(1024K)] 1135K->768K(1536K), 0.0015305 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 997K->488K(1024K)] 1277K->883K(1536K), 0.0026983 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 488K->477K(1024K)] [ParOldGen: 395K->290K(512K)] 883K->768K(1536K), [Metaspace: 2805K->2805K(1056768K)], 0.0125178 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 682K->504K(1024K)] 972K->860K(1536K), 0.0006652 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 504K->461K(1024K)] [ParOldGen: 356K->298K(512K)] 860K->760K(1536K), [Metaspace: 3032K->3032K(1056768K)], 0.0050665 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 461K->504K(1024K)] 760K->818K(1536K), 0.0022655 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 504K->448K(1024K)] [ParOldGen: 314K->294K(512K)] 818K->742K(1536K), [Metaspace: 3032K->3032K(1056768K)], 0.0053731 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at ???.main(???.java:???)
Heap
 PSYoungGen      total 1024K, used 523K [0x00000000ffe80000, 0x0000000100000000, 0x0000000100000000)
  eden space 512K, 14% used [0x00000000ffe80000,0x00000000ffe92e28,0x00000000fff00000)
  from space 512K, 87% used [0x00000000fff80000,0x00000000ffff0148,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 512K, used 294K [0x00000000ffe00000, 0x00000000ffe80000, 0x00000000ffe80000)
  object space 512K, 57% used [0x00000000ffe00000,0x00000000ffe49a48,0x00000000ffe80000)
 Metaspace       used 3132K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 338K, capacity 388K, committed 512K, reserved 1048576K

Explanation:

[GC (Allocation Failure) [PSYoungGen: 512K->488K(1024K)] 512K->496K(1536K), 0.0006315 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (分配失败) [GC类型: YoungGC前新生区大小->YoungGC后新生区大小(新生区总大小)] YoungGC前堆内存大小->YoungGC后堆内存大小(堆内存总大小), YoungGC耗时] [YoungGC耗时: 用户 系统, 实际]

[Full GC (Allocation Failure) [PSYoungGen: 504K->448K(1024K)] [ParOldGen: 314K->294K(512K)] 818K->742K(1536K), [Metaspace: 3032K->3032K(1056768K)], 0.0053731 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (分配失败) [PSYoungGen: GC前新生区大小->GC后新生区大小(新生区总大小)] [ParOldGen: GC前养老区大小->GC后养老区大小(养老区总大小)] GC前堆内存大小->GC后堆内存大小(堆内存总大小), [Metaspace: GC前元空间内存大小->GC后元空间内存大小(元空间内存总大小)], Full GC耗时] [Full GC耗时: 用户=?.?? 系统=?.??, 实际=?.?? secs]

[4] Adjust the permanent generation size

-XX:PermSize=???           设置初始永久代大小【JDK7】
-XX:MaxPermSize=???        设置最大永久代大小【JDK7】

-XX:MetaspaceSize=???      设置初始元空间大小【JDK8】
-XX:MaxMetaspaceSize=???   设置最大元空间大小【JDK8】

[5] Adjust the size of the newborn area

-XX:NewSize=???        设置新生代初始大小,等同于-Xmn
-XX:MaxNewSize=???     设置新生代最大大小

【6】Adjust the proportion of new-born area

-XX:NewRatio=2    默认为2

[7] Adjust the proportion of Eden Park

-XX:SurvivorRatio=8    默认为8

[8] Adjust the survival times of garbage objects in the new area

-XX:MaxTenuringThreshold=15    默认为15,且范围必须是在0到15之间

java version “1.8.0_211”

[9] Adjust the size of the retirement area

-XX:OldSize=???

[10] When OOM occurs, output heap memory information

-XX:+HeapDumpOnOutOfMemoryError    默认是关闭的状态

[11] Modify the heap memory Dump file path

-XX:HeapDumpPath=???

E.g:-XX:HeapDumpPath=./java_pid.hprof

[12] Adjust the initial size of heap memory

-XX:InitialHeapSize=???

Equivalent to -Xms, the default is physical memory1/64

[13] Adjust the maximum size of heap memory

-XX:MaxHeapSize=???

Equivalent to -Xmx, the default is physical memory1/4

[14] Adjust the size of the virtual machine stack thread

-XX:ThreadStackSize=???

Equivalent to-Xss

Guess you like

Origin blog.csdn.net/adsl624153/article/details/103865521
Recommended