Java堆内存溢出异常测试

来源:互联网 发布:正在识别网络 编辑:程序博客网 时间:2024/05/21 00:17

package com.sino.jvmdemo;

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

/**
 * function:Java堆内存溢出异常测试。将堆得最小值-Xms参数与最大值-Xmx参数设置为一样即可避免堆自动扩展
 * VM Args:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 * @author ylchou@qq.com
 *
 */
public class HeapOOM {
 static class OOMObject{
  
 }
 
 public static void main(String[] args) {
  List<OOMObject> list = new ArrayList<OOMObject>();
  while(true){
   list.add(new OOMObject());
  }
 }
}

/**
 
[GC [DefNew: 8192K->1024K(9216K), 0.0279878 secs] 8192K->4602K(19456K), 0.0280352 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[GC [DefNew: 6237K->1024K(9216K), 0.0314259 secs] 9815K->9739K(19456K), 0.0314631 secs] [Times: user=0.02 sys=0.02, real=0.03 secs]
[GC [DefNew: 7581K->7581K(9216K), 0.0000158 secs][Tenured: 8715K->10240K(10240K), 0.0600619 secs] 16296K->11920K(19456K), [Perm : 2086K->2086K(12288K)], 0.0601376 secs] [Times: user=0.05 sys=0.00, real=0.06 secs]
[Full GC [Tenured: 10240K->7993K(10240K), 0.0602911 secs] 19456K->15528K(19456K), [Perm : 2086K->2086K(12288K)], 0.0603326 secs] [Times: user=0.05 sys=0.00, real=0.06 secs]
[Full GC [Tenured: 8595K->8595K(10240K), 0.0613435 secs] 17811K->17811K(19456K), [Perm : 2086K->2086K(12288K)], 0.0613846 secs] [Times: user=0.06 sys=0.00, real=0.06 secs]
[Full GC [Tenured: 8595K->8592K(10240K), 0.0709844 secs] 17811K->17808K(19456K), [Perm : 2086K->2084K(12288K)], 0.0710220 secs] [Times: user=0.06 sys=0.00, real=0.07 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
 at java.util.Arrays.copyOf(Arrays.java:2760)
 at java.util.Arrays.copyOf(Arrays.java:2734)
 at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
 at java.util.ArrayList.add(ArrayList.java:351)
 at com.sino.jvmdemo.HeapOOM.main(HeapOOM.java:14)
Heap
 def new generation   total 9216K, used 9216K [0x03c90000, 0x04690000, 0x04690000)
  eden space 8192K, 100% used [0x03c90000, 0x04490000, 0x04490000)
  from space 1024K, 100% used [0x04490000, 0x04590000, 0x04590000)
  to   space 1024K,   0% used [0x04590000, 0x04590000, 0x04690000)
 tenured generation   total 10240K, used 8598K [0x04690000, 0x05090000, 0x05090000)
   the space 10240K,  83% used [0x04690000, 0x04ef5b78, 0x04ef5c00, 0x05090000)
 compacting perm gen  total 12288K, used 2105K [0x05090000, 0x05c90000, 0x09090000)
   the space 12288K,  17% used [0x05090000, 0x0529e728, 0x0529e800, 0x05c90000)
No shared spaces configured.

 */

原创粉丝点击