java面试-JVM常用的基本配置参数有哪些?

1、-Xms

  • 初始大小内存,默认为物理内存 1/64,等价于 -XX:InitialHeapSize

2、-Xmx

  • 最大分配内存,默认为物理内存的 1/4,等价于 -XX:MaxHeapSize

3、-Xss

  • 设置单个线程栈的大小,一般默认为 512-1024k,等价于 -XX:ThreadStackSize

4、-Xmn

  • 设置年轻代的大小

        整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小

        持久代一般固定大小为64m,所以增大年轻代后,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。

5、-XX:MetaspaceSize

  • 设置元空间大小

        元空间的本质和永久代类似,都是对 JVM 规范中的方法区的实现。
        元空间与永久代之间最大区别:元空间并不在虚拟机中,而是使用本地内存
        因此默认情况下,元空间的大小仅受本地内存限制,元空间默认比较小,我们可以调大一点

-Xms10m -Xmx10m -XX:MetaspaceSize=512m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseSerialGC

6、-XX:+PrintGCDetails

  • 输出详细GC收集日志信息
/** 
 *设置JVM参数为: -Xms10m -Xmx10m -XX:+PrintGCDetails
 */
public class HelloGC {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("*****helloGC");
        byte[] bytes = new byte[50 * 1024 * 1024];
    }
}

规律:[GC类型:GC前内存占用->GC后内存占用(该区内存总大小)]  GC前堆内存占用->GC后堆内存占用(JVM堆总大小),GC耗时]

GC内容:

[GC (Allocation Failure) [PSYoungGen: 1962K->512K(2560K)] 1962K->721K(9728K), 0.0015921 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 512K->496K(2560K)] 721K->755K(9728K), 0.0009185 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 496K->0K(2560K)] [ParOldGen: 259K->677K(7168K)] 755K->677K(9728K), [Metaspace: 3093K->3093K(1056768K)], 0.0056809 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 677K->677K(9728K), 0.0003427 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) Exception in thread "main" [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 677K->664K(7168K)] 677K->664K(9728K), [Metaspace: 3093K->3093K(1056768K)], 0.0052485 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
java.lang.OutOfMemoryError: Java heap space
	at com.example.demo.JVMStudy.JVMGC.HelloGC.main(HelloGC.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)Heap

 PSYoungGen      total 2560K, used 81K [0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000)
	at java.lang.reflect.Method.invoke(Method.java:498)
  eden space 2048K, 3% used [0x00000007bfd00000,0x00000007bfd14460,0x00000007bff00000)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  from space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
  to   space 512K, 0% used [0x00000007bff80000,0x00000007bff80000,0x00000007c0000000)
 ParOldGen       total 7168K, used 664K [0x00000007bf600000, 0x00000007bfd00000, 0x00000007bfd00000)
  object space 7168K, 9% used [0x00000007bf600000,0x00000007bf6a60d8,0x00000007bfd00000)
 Metaspace       used 3194K, capacity 4494K, committed 4864K, reserved 1056768K
  class space    used 350K, capacity 386K, committed 512K, reserved 1048576K 

7、-XX:SurvivorRatio

  • 设置新生代中 eden 和 S0/S1 空间比例,默认 -XX:SurvivorRatio=8,Eden : S0 : S1 = 8 : 1 : 1

8、-XX:NewRatio

  • 配置年轻代和老年代在堆结构的占比,默认 -XX:NewRatio=2 新生代占1,老年代占2,年轻代占整个堆的 1/3

9、-XX:MaxTenuringThreshold

  • 设置垃圾最大年龄
原文地址:https://www.cnblogs.com/wjh123/p/11141711.html