java堆溢出的小栗子

package com.xiaoysec.test;

import java.util.ArrayList;
import java.util.List;


/**
 *VM Args:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 * @author xiaoysec
 *
 */
public class HeapOOM {
    static class OOMObject{
        //定义一个静态内部类
    }
    public static void main(String[] args){
        List<OOMObject> list = new ArrayList<HeapOOM.OOMObject>();
        //int i=0;
        while(true){
            list.add(new OOMObject());
            //System.out.println(i);
            //i++;
        }
    }
}

代码是相当简单的,就是一个HeapOOM类进行HeapOutofMemory测试,在该类里面有一个静态内部类OOMObject

 

主要的思想就是在main方法里面用一个死循环不停地创建对象我们知道java对象是在java堆里面的当堆内存不足的时候虚拟机会进行垃圾回收,但是当虚拟机发现这些对象还是有用的不能被回收时,就扩展堆空间,但在本例当中有一个while(true)也就是说即使扩展了空间也是没用的!

值得一提的是我们需要在VM arguments中设置 -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8也就是把堆的最大最小值设置为相同的20M以避免堆进行自动扩展

下面就是运行结果:

[GC (Allocation Failure) [PSYoungGen: 7882K->1000K(9216K)] 7882K->5433K(19456K), 0.0100414 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) --[PSYoungGen: 9192K->9192K(9216K)] 13625K->19424K(19456K), 0.0155543 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 9192K->0K(9216K)] [ParOldGen: 10232K->10051K(10240K)] 19424K->10051K(19456K), [Metaspace: 2756K->2756K(1056768K)], 0.2144901 secs] [Times: user=0.36 sys=0.00, real=0.21 secs] 
[Full GC (Ergonomics) [PSYoungGen: 7635K->8076K(9216K)] [ParOldGen: 10051K->7940K(10240K)] 17687K->16017K(19456K), [Metaspace: 2756K->2756K(1056768K)], 0.1848934 secs] [Times: user=0.37 sys=0.00, real=0.18 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 8076K->8076K(9216K)] [ParOldGen: 7940K->7940K(10240K)] 16017K->16017K(19456K), [Metaspace: 2756K->2756K(1056768K)], 0.1011278 secs] [Times: user=0.33 sys=0.00, real=0.10 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at com.xiaoysec.test.HeapOOM.main(HeapOOM.java:20)
[Full GC (Ergonomics) [PSYoungGen: 8192K->0K(9216K)] [ParOldGen: 7940K->656K(10240K)] 16132K->656K(19456K), [Metaspace: 2781K->2781K(1056768K)], 0.0056287 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 9216K, used 82K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 1% used [0x00000000ff600000,0x00000000ff614938,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 10240K, used 656K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 6% used [0x00000000fec00000,0x00000000feca4310,0x00000000ff600000)
 Metaspace       used 2787K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 307K, capacity 386K, committed 512K, reserved 1048576K

posted @ 2015-03-17 18:58  小羊同学  阅读(1503)  评论(0编辑  收藏  举报