Java堆溢出: java.lang.OutOfMemoryError: Java heap space
- /**
- * VM Args:-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
- * @author zzm
- */
- ublic class HeapOOM {
- static class OOMObject {
- }
- public static void main(String[] args) {
- List<OOMObject> list = new ArrayList<OOMObject>();
- while (true) {
- list.add(new OOMObject());
- }
- }
- }
运行结果:
- java.lang.OutOfMemoryError: Java heap space
- Dumping heap to java_pid3404.hprof ...
- Heap dump file created [22045981 bytes in 0.663 secs]
http://book.51cto.com/art/201107/278889.htm
虚拟机栈和本地方法栈溢出
(java.lang.StackOverflowError)
- stack length:2402
- Exception in thread "main" java.lang.StackOverflowError
http://book.51cto.com/art/201107/278890.htm
运行时常量池溢出
(OutOfMemoryError: PermGen space at org.fenixsoft.oom.RuntimeConstantPoolOOM)
- Exception in thread "main" java.lang.
- [b]OutOfMemoryError: PermGen space[/b]
- at java.lang.String.intern(Native Method)
- at org.fenixsoft.oom.[b]RuntimeConstantPoolOOM[/b].
- main(RuntimeConstantPoolOOM.java:18)
http://book.51cto.com/art/201107/278891.htm
方法区溢出
(Caused by: java.lang.OutOfMemoryError: PermGen space )
- Caused by: [b]java.lang.OutOfMemoryError: PermGen space [/b]
- at java.lang.ClassLoader.defineClass1(Native Method)
- at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
- at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
- ... 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上