3、下默认装载用户类,在SunJava1.5中既sun.misc.Launcher$AppClassLoader。更详细的内容请参看下面的资料。准备heapdump请看下面的Pilot类,没啥特殊的。/** * Pilot class * @author rosen jiang */package org.rosenjiang.bo;public class Pilot{ String name; int age; public Pilot(String a, int b){ name
4、 = a; age = b; }}然后再看OOMHeapTest类,它是如何撑破heapdump的。/** * OOMHeapTest class * @author rosen jiang */package org.rosenjiang.test;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.rosenjiang.bo.Pilot;public class OOMHeapTest { pu
5、blic static void main(String[] args){ oom(); } private static void oom(){ Map map = new HashMap(); Object[] array = new Object[1000000]; for(int i=0; i<1000000; i++){ String d = new Date()
6、.toString(); Pilot p = new Pilot(d, i); map.put(i+"rosen jiang", p); array[i]=p; } }}是的,上面构造了很多的Pilot类实例,向数组和map中放。由于是StrongRef,GC自然不会回收这些对象,一直放在heap中直到溢出。当然在运行前,先要在Eclipse中配置VM参数-XX:+HeapDumpOnOutOfMemoryError。好了,一会儿功夫内存溢出,
7、控制台打出如下信息。java.lang.OutOfMemoryError: Java heap spaceDumping heap to java_pid3600.hprof Heap dump file created [78233961 bytes in 1.995 secs]Exception in thread "main" java.lang.OutOfMemoryError: Java heap spacejava_pid3600.hprof既是heapdump,可以在OOMHeapTest类所在的工程根目录下找到。