jvm GC详细参数设置

VM  GC 日志参数:

-verbose:gc
-XX:+HeapDumpOnOutOfMemoryError
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+PrintGCDateStamps
-Xloggc:/appl/gclogs/gc.log
备注:-Xloggc的目录需要提前建好。

我们模拟一个程序产生堆内存空间异常
import java.util.Vector;

public class Test5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        
        Vector v =new Vector();
        for(int i =0 ;i<=10;i++){
            //每次产生1M的内存空间
            byte[] b = new byte[1024*1024];
            v.add(b);
            System.out.println("i是:"+i);
        }
        //获取当前系统能够获得最大内存空间
        long maxMemory = Runtime.getRuntime().maxMemory()/1024/1024;
        System.out.println("当前系统能够使用的最大内存空间是:"+maxMemory+"M");
    }

}

我们设置改程序的最大堆为5M,程序中每次循环产生1M,一共有10M就会产生堆异常

-Xmx5M -Xms5M -verbose:gc -XX:+HeapDumpOnOutOfMemoryError -Xloggc:gc.log

XX:+HeapDumpOnOutOfMemoryError在产生堆异常的时候会产生一个堆异常文件 java_pid4260.hprof

-Xloggc:gc.log记录整个gc过程的异常日志

i是:0
i是:1
i是:2
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid4260.hprof ...
Heap dump file created [4396657 bytes in 0.012 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Test5.main(Test5.java:12)

原文地址:https://www.cnblogs.com/kebibuluan/p/15399590.html