HeapDumpOnOutOfMemoryError堆转储实践和一些分析
public static void main(String[] args) { long arr[]; for (int i=1; i<=10000000; i*=2) { arr = new long[i]; }}
public static void main(String[] args){ for (int i = 1; i <= 10000000; i *= 2) { long[] arr = new long[i]; }}
System.out.println("size : " + i);Runtime runtime = Runtime.getRuntime();System.out.printf("maxMemory : %.2fM\n", runtime.maxMemory()*1.0/1024/1024);System.out.printf("totalMemory : %.2fM\n", runtime.totalMemory()*1.0/1024/1024);System.out.printf("freeMemory : %.2fM\n", runtime.freeMemory()*1.0/1024/1024);
public static void main(String[] args) { long arr[] = {}; for (int i=1; i<=10000000; i*=2) { arr = new long[i]; }}
public static void main(String[] args){ long[] arr = new long[0]; for (int i = 1; i <= 10000000; i *= 2) { arr = new long[i]; }}
?
小结:通过分析OutOfMemoryError时生成的堆转储文件,有助于找到内存不够用的原因;如果生成的堆转储文件的大小跟最大堆内存的配置有很大差异,就要分析抛出异常的代码,查找原因,还可以将字节码反编译,看看编译器是不是对代码的结构进行了调整。