StackOverflowError(java.lang.StackOverflowError):
/**
* @author luffy
**/
public class OOMTest {
public static void main(String[] args){
stackOverFlowError();
}
public static void stackOverFlowError(){
stackOverFlowError();
}
}
Java heap space(java.lang.OutOfMemoryError: Java heap space):
/**
* @author luffy
**/
public class OOMTest {
//-Xmx10m
public static void main(String[] args){
Byte[] bytes = new Byte[100*1024*1024];
}
}
GC回收时间过长会抛出OOM,超过98%的时间用来做GC,但回收了不到2%的堆内存。GC overhead limit exceeded(java.lang.OutOfMemoryError: GC overhead limit exceeded)
/**
* @author luffy
**/
public class OOMTest {
//-Xmx10m -Xms10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
public static void main(String[] args){
int i =0;
List<String> list = new ArrayList<>();
try {
while (true){
list.add(String.valueOf(i++).intern());
}
} catch (Throwable e) {
System.out.println(i);
e.printStackTrace();
throw e;
}
}
}
Direct buffer memory(java.lang.OutOfMemoryError: Direct buffer memory):在本地内存,不在GC的回收范围内。(NIO)
/**
* @author luffy
**/
public class OOMTest {
// -XX:MaxDirectMemorySize=5m
public static void main(String[] args){
System.out.println(sun.misc.VM.maxDirectMemory()/(1024*1024*1.0)+"G");
ByteBuffer.allocateDirect(20*1024*1024);
}
}
(unable to create new native thread)java.lang.OutOfMemoryError: unable to create new native thread
/**
* @author luffy
**/
public class OOMTest {
//1024个线程
public static void main(String[] args){
for (int i=0;;i++){
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
java.lang.OutOfMemoryError: MetaSpace(Java8以后替换永久代)是方法区在HotSpot的实现,不在虚拟机内存内,直接使用本地内存。存放以下信息:
- 虚拟机加载的类信息
- 常量池
- 静态变量
- 即时编译后的代码
网友评论