OutOfMemoryError溢出

Java堆溢出: java.lang.OutOfMemoryError: Java heap space 

  1. /**  
  2. * VM Args:-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError  
  3. * @author zzm  
  4. */  
  5. ublic class HeapOOM {  
  6. static class OOMObject {  
  7. }  
  8. public static void main(String[] args) {  
  9.     List<OOMObject> list = new ArrayList<OOMObject>();  
  10.     while (true) {  
  11.         list.add(new OOMObject());  
  12.     }  
  13. }  

运行结果:

  1. java.lang.OutOfMemoryError: Java heap space  
  2. Dumping heap to java_pid3404.hprof ...  
  3. Heap dump file created [22045981 bytes in 0.663 secs] 


http://book.51cto.com/art/201107/278889.htm 

 

 

虚拟机栈和本地方法栈溢出

(java.lang.StackOverflowError)  

Java代码 
  1. stack length:2402    
  2. Exception in thread "main" java.lang.StackOverflowError   


http://book.51cto.com/art/201107/278890.htm 

 

运行时常量池溢出 

(OutOfMemoryError: PermGen space  at org.fenixsoft.oom.RuntimeConstantPoolOOM) 

Java代码 
  1. Exception in thread "main" java.lang.  
  2. [b]OutOfMemoryError: PermGen space[/b]    
  3. at java.lang.String.intern(Native Method)    
  4. at org.fenixsoft.oom.[b]RuntimeConstantPoolOOM[/b].  
  5. main(RuntimeConstantPoolOOM.java:18)   


http://book.51cto.com/art/201107/278891.htm 


方法区溢出

(Caused by: java.lang.OutOfMemoryError: PermGen space  ) 

Java代码 
  1. Caused by: [b]java.lang.OutOfMemoryError: PermGen space  [/b]  
  2. at java.lang.ClassLoader.defineClass1(Native Method)    
  3. at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)    
  4. at java.lang.ClassLoader.defineClass(ClassLoader.java:616)    
  5. ... 8 more   


在经常动态生成大量Class的应用中,需要特别注意类的回收状况。这类场景除了上面提到的程序使用了GCLib字节码增强外,常见的还有:大量JSP或动态产生JSP文件的应用(JSP第一次运行时需要编译为Java类)、基于OSGi的应用(即使是同一个类文件,被不同的加载器加载也会视为不同的类)等。 
http://book.51cto.com/art/201107/278894.htm 



本机直接内存溢出

(Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory)

 

public class Test {
	public static void main(String[] args) {
		for (int i = 0; i < 10000; i++) {
			createThread();
			System.out.println(i);
		}
	}

	private static void createThread() {
		ByteBuffer bf = ByteBuffer.allocateDirect(1024 *1024 *100);
	}
}
 gc 日志:
2017-05-14T20:02:37.074+0800: [GC [PSYoungGen: 717K->568K(5952K)] 717K->568K(19648K), 0.0060602 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2017-05-14T20:02:37.081+0800: [Full GC (System) [PSYoungGen: 568K->0K(5952K)] [ParOldGen: 0K->485K(13696K)] 568K->485K(19648K) [PSPermGen: 2527K->2525K(21248K)], 0.0097560 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:658)
	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306)
	at Test.createThread(Test.java:22)
	at Test.main(Test.java:7)
 
注意: 对于 DirectByteBuffer 因为不是在虚拟机分配的,full gc 日志 体现不了 日志回收的情况
这样分配的内存不再是在java heap上,而是在C heap上
2017-05-14T20:02:37.074+0800: [GC [PSYoungGen: 717K->568K(5952K)] 717K->568K(19648K), 0.0060602 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2017-05-14T20:02:37.081+0800: [Full GC (System) [PSYoungGen: 568K->0K(5952K)] [ParOldGen: 0K->485K(13696K)] 568K->485K(19648K) [PSPermGen: 2527K->2525K(21248K)], 0.0097560 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:658)
	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306)
	at Test.createThread(Test.java:22)
	at Test.main(Test.java:7)
  注意: 对于 DirectByteBuffer 因为不是在虚拟机分配的,full gc 日志 体现不了 日志回收的情况 这样分配的内存不再是在java heap上,而是在C heap上 http://book.51cto.com/art/201107/278895.htm

猜你喜欢

转载自wangxinchun.iteye.com/blog/2287306