常用JVM配置参数

时间:2023-03-09 01:30:32
常用JVM配置参数

常用JVM配置参数

  • Trace跟踪参数
  • 堆的分配参数
  • 栈的分配参数

Trace跟踪参数

1.打开GC的日志,如果在程序的运行过程中,系统发生了GC,就会打印相关的信息。

-verbose:gc
-XX:+printGC
可以打印GC的简要信息//0.0001606 secs垃圾收集消耗的时间//回收前占据的堆的空间是4790k,回收后占据374k,相减,回收了多少垃圾//整个堆的大小是15872k
[GC 4790K->374K(15872K), 0.0001606 secs]
[GC 4790K->374K(15872K), 0.0001474 secs]
[GC 4790K->374K(15872K), 0.0001563 secs]
[GC 4790K->374K(15872K), 0.0001682 secs]

2.上述打印的内容比较简单,如果要打印出GC的详细信息,

-XX:+PrintGCDetails
打印GC详细信息
-XX:+PrintGCTimeStamps
打印CG发生的时间戳
例//新生代的GC,回收前占4416k,回收之后清空,新生代总大小4928k。
[GC[DefNew: 4416K->0K(4928K), 0.0001897 secs] 4790K->374K(15872K), 0.0002232 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
-XX:+PrintGCDetails的输出
Heap
 def new generation  total 13824K, used 11223K [0x27e80000, 0x28d80000, 0x28d80000)
 eden space 12288K,  91% used [0x27e80000, 0x28975f20, 0x28a80000)
 from space 1536K,   0% used [0x28a80000, 0x28a80000, 0x28c00000)
 to space 1536K,   0% used [0x28c00000, 0x28c00000, 0x28d80000)
 tenured generation  total 5120K, used 0K [0x28d80000, 0x29280000, 0x34680000)
 the space 5120K,   0% used [0x28d80000, 0x28d80000, 0x28d80200, 0x29280000)
 compacting perm gen  total 12288K, used 142K [0x34680000, 0x35280000, 0x38680000)
 the space 12288K,   1% used [0x34680000, 0x346a3a90, 0x346a3c00, 0x35280000)
 ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)
 rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)

GC日志重定向

-Xloggc:log/gc.log
指定GC log的位置,以文件输出
帮助开发人员分析问题

常用JVM配置参数

-----

-XX:+PrintHeapAtGC
每次一次GC后,都打印堆信息
{
Heap before GC invocations=0 (full 0):
 def new generation   total 3072K, used 2752K [0x33c80000, 0x33fd0000, 0x33fd0000)
 eden space 2752K, 100% used [0x33c80000, 0x33f30000, 0x33f30000)
 from space 320K,   0% used [0x33f30000, 0x33f30000, 0x33f80000)
 to space 320K,   0% used [0x33f80000, 0x33f80000, 0x33fd0000)
 tenured generation   total 6848K, used 0K [0x33fd0000, 0x34680000, 0x34680000)
 the space 6848K,   0% used [0x33fd0000, 0x33fd0000, 0x33fd0200, 0x34680000)
 compacting perm gen  total 12288K, used 143K [0x34680000, 0x35280000, 0x38680000)
 the space 12288K,   1% used [0x34680000, 0x346a3c58, 0x346a3e00, 0x35280000)
 ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)
 rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)
[GC[DefNew: 2752K->320K(3072K), 0.0014296 secs] 2752K->377K(9920K), 0.0014604 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=1 (full 0):
 def new generation   total 3072K, used 320K [0x33c80000, 0x33fd0000, 0x33fd0000)
 eden space 2752K,   0% used [0x33c80000, 0x33c80000, 0x33f30000)
 from space 320K, 100% used [0x33f80000, 0x33fd0000, 0x33fd0000)
 to space 320K,   0% used [0x33f30000, 0x33f30000, 0x33f80000)
 tenured generation   total 6848K, used 57K [0x33fd0000, 0x34680000, 0x34680000)
 the space 6848K,   0% used [0x33fd0000, 0x33fde458, 0x33fde600, 0x34680000)
 compacting perm gen  total 12288K, used 143K [0x34680000, 0x35280000, 0x38680000)
 the space 12288K,   1% used [0x34680000, 0x346a3c58, 0x346a3e00, 0x35280000)
 ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)
 rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)
}

3.跟踪类的被加载的过程

-XX:+TraceClassLoading
监控类的加载
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]

4.打印类的使用情况

XX:+PrintClassHistogram
在控制台按下Ctrl+Break后,打印类的信息:
 num     #instances         #bytes  class name
----------------------------------------------
   1:        890617      470266000  [B
   2:        890643       21375432  java.util.HashMap$Node
   3:        890608       14249728  java.lang.Long
   4:            13        8389712  [Ljava.util.HashMap$Node;
   5:          2062         371680  [C
   6:           463          41904  java.lang.Class
分别显示:序号、实例数量、总大小、类

所有的类的使用情况。
1:890617:470266000:[B
-->Byte数组,有890617个Byte数组对象,它们总共占了470266000字节的空间。
堆的分配参数

-Xmx –Xms
指定最大堆和最小堆
//JVM最多使用20M空间,系统一启动就占用5M空间
-Xmx20m -Xms5m  运行代码:
public class Test {
    public static void main(String[] args) {
        System.out.print("Xmx=");
        System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");

        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");

        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");

    }
}

常用JVM配置参数

常用JVM配置参数

public class Test {
    public static void main(String[] args) {
        byte[] b=new byte[1*1024*1024];
        System.out.println("分配了1M空间给数组");

        System.out.print("Xmx=");
        System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");

        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");

        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
    }
}

常用JVM配置参数

我们看到,总内存没变,这里涉及一个原则:Java会尽可能维持在最小堆(Xms5m)

但是当内存中对象比较大,最小堆无法满足要求时,总内存就会变大。

public class Test {
    public static void main(String[] args) {
        byte[] b=new byte[4*1024*1024];
        System.out.println("分配了4M空间给数组");

        System.out.print("Xmx=");
        System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");

        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");

        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");

    }
}

常用JVM配置参数

可以看到,总内存变大了。

在前面两个字节数组的基础上,手动进行垃圾回收,

public class Test {
    public static void main(String[] args) {
        System.gc();

        System.out.print("Xmx=");
        System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");

        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");

        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");

    }
}

常用JVM配置参数

维持在最小堆(Xms5m)

------

-Xmn
设置新生代大小
-XX:NewRatio
新生代(eden+2*s)和老年代(不包含永久区)的比值
4 表示 新生代:老年代=1:4,即年轻代占堆的1/5
-XX:SurvivorRatio
设置两个Survivor区和eden的比
8表示 两个Survivor :eden=2:8,即一个Survivor占年轻代的1/10
public class Test {
    public static void main(String[] args) {
        byte[] b=null;
           for(int i=0;i<10;i++)
               b=new byte[1*1024*1024];
    }
}

常用JVM配置参数

Heap
 def new generation   total 960K, used 551K [0x327a0000, 0x328a0000, 0x328a0000)
 eden space 896K,  61% used [0x327a0000, 0x32829e68, 0x32880000)
 from space 64K,   0% used [0x32880000, 0x32880000, 0x32890000)
 to space 64K,   0% used [0x32890000, 0x32890000, 0x328a0000)
 tenured generation   total 19456K, used 10240K [0x328a0000, 0x33ba0000, 0x33ba0000)
 the space 19456K,  52% used [0x328a0000, 0x332a00a0, 0x332a0200, 0x33ba0000)
 compacting perm gen  total 12288K, used 145K [0x33ba0000, 0x347a0000, 0x37ba0000)
 the space 12288K,   1% used [0x33ba0000, 0x33bc44f0, 0x33bc4600, 0x347a0000)
 ro space 10240K,  41% used [0x37ba0000, 0x37fd2050, 0x37fd2200, 0x385a0000)
 rw space 12288K,  52% used [0x385a0000, 0x38be6800, 0x38be6800, 0x391a0000)
1.没有触发GC
2.全部分配在老年代

常用JVM配置参数

Heap
 def new generation   total 13824K, used 11223K [0x327a0000, 0x336a0000, 0x336a0000)
 eden space 12288K,  91% used [0x327a0000, 0x33295f00, 0x333a0000)
 from space 1536K,   0% used [0x333a0000, 0x333a0000, 0x33520000)
 to space 1536K,   0% used [0x33520000, 0x33520000, 0x336a0000)
 tenured generation   total 5120K, used 0K [0x336a0000, 0x33ba0000, 0x33ba0000)
 the space 5120K,   0% used [0x336a0000, 0x336a0000, 0x336a0200, 0x33ba0000)
 compacting perm gen  total 12288K, used 145K [0x33ba0000, 0x347a0000, 0x37ba0000)
 the space 12288K,   1% used [0x33ba0000, 0x33bc44f0, 0x33bc4600, 0x347a0000)
 ro space 10240K,  41% used [0x37ba0000, 0x37fd2050, 0x37fd2200, 0x385a0000)
 rw space 12288K,  52% used [0x385a0000, 0x38be6800, 0x38be6800, 0x391a0000)
1.没有触发GC
2.全部分配在eden
3.老年代没有使用

常用JVM配置参数

[GC[DefNew: 5737K->372K(6464K), 0.0022402 secs] 5737K->1396K(19776K), 0.0022938 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
 def new generation   total 6464K, used 5786K [0x327a0000, 0x32ea0000, 0x32ea0000)
 eden space 5760K,  93% used [0x327a0000, 0x32ce9848, 0x32d40000)
 from space 704K,  52% used [0x32df0000, 0x32e4d068, 0x32ea0000)
 to space 704K,   0% used [0x32d40000, 0x32d40000, 0x32df0000)
 tenured generation   total 13312K, used 1024K [0x32ea0000, 0x33ba0000, 0x33ba0000)
 the space 13312K,   7% used [0x32ea0000, 0x32fa0010, 0x32fa0200, 0x33ba0000)
 compacting perm gen  total 12288K, used 145K [0x33ba0000, 0x347a0000, 0x37ba0000)
 the space 12288K,   1% used [0x33ba0000, 0x33bc44f0, 0x33bc4600, 0x347a0000)
 ro space 10240K,  41% used [0x37ba0000, 0x37fd2050, 0x37fd2200, 0x385a0000)
 rw space 12288K,  52% used [0x385a0000, 0x38be6800, 0x38be6800, 0x391a0000)

1.进行了1次新生代GC

2.s0 s1 太小需要老年代担保

常用JVM配置参数

[GC[DefNew: 2583K->1396K(5376K), 0.0023742 secs] 2583K->1396K(18688K), 0.0024289 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC[DefNew: 4582K->1024K(5376K), 0.0017806 secs] 4582K->1395K(18688K), 0.0018160 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC[DefNew: 4124K->1024K(5376K), 0.0007338 secs] 4495K->1395K(18688K), 0.0007731 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
 def new generation   total 5376K, used 3162K [0x327a0000, 0x32ea0000, 0x32ea0000)
 eden space 3584K,  59% used [0x327a0000, 0x329b6960, 0x32b20000)
 from space 1792K,  57% used [0x32ce0000, 0x32de0010, 0x32ea0000)
 to space 1792K,   0% used [0x32b20000, 0x32b20000, 0x32ce0000)
 tenured generation   total 13312K, used 371K [0x32ea0000, 0x33ba0000, 0x33ba0000)
 the space 13312K,   2% used [0x32ea0000, 0x32efcf00, 0x32efd000, 0x33ba0000)
 compacting perm gen  total 12288K, used 145K [0x33ba0000, 0x347a0000, 0x37ba0000)
 the space 12288K,   1% used [0x33ba0000, 0x33bc44f0, 0x33bc4600, 0x347a0000)

1.进行了3次新生代GC

2.s0 s1 增大

常用JVM配置参数

[GC[DefNew: 4625K->1396K(7680K), 0.0030680 secs] 4625K->1396K(17920K), 0.0031348 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC[DefNew: 5651K->1024K(7680K), 0.0022846 secs] 5651K->1395K(17920K), 0.0023257 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
 def new generation   total 7680K, used 3212K [0x327a0000, 0x331a0000, 0x331a0000)
 eden space 5120K,  42% used [0x327a0000, 0x329c2fe8, 0x32ca0000)
 from space 2560K,  40% used [0x32ca0000, 0x32da0098, 0x32f20000)
 to space 2560K,   0% used [0x32f20000, 0x32f20000, 0x331a0000)
 tenured generation   total 10240K, used 371K [0x331a0000, 0x33ba0000, 0x33ba0000)
 the space 10240K,   3% used [0x331a0000, 0x331fce78, 0x331fd000, 0x33ba0000)
 compacting perm gen  total 12288K, used 145K [0x33ba0000, 0x347a0000, 0x37ba0000)
 the space 12288K,   1% used [0x33ba0000, 0x33bc44f0, 0x33bc4600, 0x347a0000)
 ro space 10240K,  41% used [0x37ba0000, 0x37fd2050, 0x37fd2200, 0x385a0000)

比例分配,新生代 老年代对半开

对象全部留在新生代

常用JVM配置参数

[GC[DefNew: 5751K->1396K(8192K), 0.0022373 secs] 5751K->1396K(18432K), 0.0022949 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
Heap
 def new generation   total 8192K, used 6830K [0x327a0000, 0x331a0000, 0x331a0000)
 eden space 6144K,  88% used [0x327a0000, 0x32cee838, 0x32da0000)
 from space 2048K,  68% used [0x32fa0000, 0x330fd078, 0x331a0000)
 to space 2048K,   0% used [0x32da0000, 0x32da0000, 0x32fa0000)
 tenured generation   total 10240K, used 0K [0x331a0000, 0x33ba0000, 0x33ba0000)
 the space 10240K,   0% used [0x331a0000, 0x331a0000, 0x331a0200, 0x33ba0000)
 compacting perm gen  total 12288K, used 145K [0x33ba0000, 0x347a0000, 0x37ba0000)
 the space 12288K,   1% used [0x33ba0000, 0x33bc44f0, 0x33bc4600, 0x347a0000)
 ro space 10240K,  41% used [0x37ba0000, 0x37fd2050, 0x37fd2200, 0x385a0000)
 rw space 12288K,  52% used [0x385a0000, 0x38be6800, 0x38be6800, 0x391a0000)

减少了s0 s1 GC数量变少,老年代未使用 空间使用率更高

-XX:+HeapDumpOnOutOfMemoryError
OOM时导出堆到文件
-XX:+HeapDumpPath
导出OOM的路径
-Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/a.dump
public class Test {
    public static void main(String[] args) {
         Vector v=new Vector();
            for(int i=0;i<25;i++)
                v.add(new byte[1*1024*1024]);
    }
}

常用JVM配置参数

常用JVM配置参数

堆的分配参数 – 总结

--根据实际事情调整新生代和幸存代的大小
--官方推荐新生代占堆的3/8
--幸存代占新生代的1/10
--在OOM时,记得Dump出堆,确保可以排查现场问题

永久区分配参数

-XX:PermSize  -XX:MaxPermSize
设置永久区的初始空间和最大空间
他们表示,一个系统可以容纳多少个类型
使用CGLIB等库的时候,可能会产生大量的类,这些类,有可能撑爆永久区导致OOM
for(int i=0;i<100000;i++){
    CglibBean bean = new CglibBean("geym.jvm.ch3.perm.bean"+i,new HashMap());
}

栈大小分配

-Xss
通常只有几百K
决定了函数调用的深度
每个线程都有独立的栈空间
局部变量、参数 分配在栈上
public class TestStackDeep {
    private static int count=0;
    public static void recursion(long a,long b,long c){
        long e=1,f=2,g=3,h=4,i=5,k=6,q=7,x=8,y=9,z=10;
        count++;
        recursion(a,b,c);
    }
    public static void main(String args[]){
        try{
            recursion(0L,0L,0L);
        }catch(Throwable e){
            System.out.println("deep of calling = "+count);
            e.printStackTrace();
        }
    }
}
递归调用
-Xss128K
deep of calling = 701
java.lang.*Error

-Xss256K
deep of calling = 1817
java.lang.*Error

去掉局部变量 调用层次可以更深