package jvm.memory;
import java.util.ArrayList;
import java.util.List;
/**
* {@link HeapOOM}
*
* 测试Java堆溢出
* OOM -> OutOfMemoryError
*
* VM Args:-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
*
* @author yameing
*/
public class HeapOOM {
static class OOMObject {
int i = 1 ;
}
public static void main(String[] args) {
List list = new ArrayList() ;
while (true) {
list.add(new OOMObject()) ;
}
}
}
E:\Test\src>java -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError jvm.memory.HeapOOM
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid4236.hprof ...
Heap dump file created [28780941 bytes in 0.501 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 jvm.memory.HeapOOM.main(HeapOOM.java:31)
通过内存映像分析工具(如Eclipse Memory Analyzer)对dump出来的堆存储快照进行分析, 重点是确认内存中的对象是否是必要的,也就是要分清楚到底是出现了内存泄露(Memory Leak)还是内存溢出(Memory Overflow)。内存泄露: 一般来说内存泄漏有两种情况。一种情况如在C/C++语言中的,在堆中的分配的内存,在没有将其释放掉的时候,就将所有能访问这块内存的方式都删掉(如指针重新赋值);另一种情况则是在内存对象明明已经不需要的时候,还仍然保留着这块内存和它的访问方式(引用)。第一种情况,在Java中已经由于垃圾回收机制的引入,得到了很好的解决。所以,Java中的内存泄漏,主要指的是第二种情况。(无用的对象过多)
1 、查看泄露对象到 GC Roots 的引用链2 、确定导致 GC 无法自动回收他们的原因3 、定位出泄露代码的位置内存溢出:存在无法回收的内存或使用的内存过多,最终使得程序运行要用到的内存大于虚拟机能提供的最大内存。(有用的对象过多)1、检查虚拟机的堆参数(-Xms与-Xmx)2、检查是否某些对象生命周期过长
a、虚拟机栈和本地方法栈StackOverFlow测试:
package jvm.memory;
/**
* {@link JavaVMStackSOF}
*
* 虚拟机栈和本地方法栈StackOverFlow测试
*
* VM Args:-Xss128k
*
* @author yameing
*/
public class JavaVMStackSOF {
private int stackLength = 1 ;
public void stackLeak() {
stackLength ++ ;
stackLeak() ;
}
public static void main(String[] args) {
JavaVMStackSOF oom = new JavaVMStackSOF() ;
try {
oom.stackLeak() ;
} catch (Exception e) {
System.out.println("stack length : " + oom.stackLength);
e.printStackTrace();
}
}
}
E:\Test\src>java -Xss128k jvm.memory.JavaVMStackSOF
stack length : 2401
Exception in thread "main" java.lang.StackOverflowError
at jvm.memory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:22)
at jvm.memory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:23)
at jvm.memory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:23)
package jvm.memory;
/**
* {@link JavaVMStackOOM}
*
* 模拟虚拟机栈内存溢出
* Args:-Xss2M
*
* @author Administrator
*/
public class JavaVMStackOOM {
/**
* 不断循环,不让线程结束
*/
public void donotStop() {
while(true) {}
}
/**
* 不断创建新线程
*/
public void stackLeakByThread() {
while(true) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
donotStop() ;
}
}) ;
t.start() ;
}
}
public static void main(String[] args) {
JavaVMStackOOM oom = new JavaVMStackOOM() ;
oom.stackLeakByThread() ;
}
}
E:\Test\src>java -Xss128k jvm.memory.JavaVMStackOOM
Exception in thread "main" java.lang.OutOfMemoryError:unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:597)
at jvm.memory.JavaVMStackOOM.stackLeakByThread(JavaVMStackOOM.java:34)
at jvm.memory.JavaVMStackOOM.main(JavaVMStackOOM.java:40)
package jvm.memory;
import java.util.List;
import java.util.ArrayList;
/**
* {@link RuntimeConstantPoolOOM}
*
* VM Args:-XX:PermSize=10M -XX:MaxPermSize=10M
*
* @author yameing
*/
public class RuntimeConstantPoolOOM {
public static void main(String[] args) throws Throwable {
List list = new ArrayList() ;
int i = 0 ;
while(true) {
list.add(String.valueOf(i++).intern()) ;
}
}
}
E:Test\src>java -XX:PermSize=10M -XX:MaxPermSize=10M jvm.memory.RuntimeConstantPoolOOM
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
at java.lang.String.intern(Native Method)
at jvm.memory.RuntimeConstantPoolOOM.main(RuntimeConstantPoolOOM.java:26)
package jvm.memory;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
/**
* {@link DirectMemoryOOM}
*
* VM Args:-Xmx20M -XX:MaxDirectMemorySize=10M
*
* @author yameing
*/
public class DirectMemoryOOM {
private static final int _1MB = 1024 * 1024 ;
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
Field unsafeField = Unsafe.class.getDeclaredFields()[0] ;
unsafeField.setAccessible(true) ;
Unsafe unsafe = (Unsafe) unsafeField.get(null) ;
while(true) {
unsafe.allocateMemory(_1MB) ;
}
}
}
E:\bingosoft\project\Test\src>java -Xmx20M -XX:MaxDirectMemorySize=10M jvm.memory.DirectMemoryOOM
Exception in thread "main" java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at jvm.memory.DirectMemoryOOM.main(DirectMemoryOOM.java:28)