-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
import java.util.ArrayList;
import java.util.List;
public class OOM {
static class OOMObject{}
public static void main(String[] args) {
List<OOMObject> list = new ArrayList<>();
while(true){
list.add(new OOMObject());
}
}
}
*明确允许Java虚拟机自行选择是否支持栈的动态拓展 HotSpot虚拟机是不支持拓展的
- 所以说对于OOM来说,除非在创建线程的时候申请内存就超出了。不然的话在线程运行时,不会因为拓展而导致内存溢出。
/**
* VM Args: -Xss128k
*/
public class JavaVMStackSOF {
private int mStackLength = 1;
public void stackLeak(){
mStackLength++;
stackLeak();
}
public static void main(String[] args) {
JavaVMStackSOF oom = new JavaVMStackSOF();
try{
oom.stackLeak();
}catch (Throwable e){
System.out.println("stack length:" +oom.mStackLength);
throw e;
}
}
}
public class JavaVMStackSOF{
private static int stackLength = 0;
public static void stackLeak(){
long unused1,unused2,unused3,unused4,unused5,unused6,unused7,unused8,unused9,unused10,
unused11,unused12,unused13,unused14,unused15,unused16,unused17,unused18,unused19,unused20,
unused21,unused22,unused23,unused24,unused25,unused26,unused27,unused28,unused29,unused30,
unused31,unused32,unused33,unused34,unused35,unused36,unused37,unused38,unused39,unused40,
unused41,unused42,unused43,unused44,unused45,unused46,unused47,unused48,unused49,unused50,
unused51,unused52,unused53,unused54,unused55,unused56,unused57,unused58,unused59,unused60,
unused61,unused62,unused63,unused64,unused65,unused66,unused67,unused68,unused69,unused70,
unused71,unused72,unused73,unused74,unused75,unused76,unused77,unused78,unused79,unused80,
unused81,unused82,unused83,unused84,unused85,unused86,unused87,unused88,unused89,unused90,
unused91,unused92,unused93,unused94,unused95,unused96,unused97,unused98,unused99,unused100;
stackLength++;
stackLeak();
unused1=unused2=unused3=unused4=unused5=unused6=unused7=unused8=unused9=unused10=
unused11=unused12=unused13=unused14=unused15=unused16=unused17=unused18=unused19=unused20=
unused21=unused22=unused23=unused24=unused25=unused26=unused27=unused28=unused29=unused30=
unused31=unused32=unused33=unused34=unused3=unused36=unused37=unused38=unused39=unused40=
unused41=unused42=unused43=unused44=unused45=unused46=unused47=unused48=unused49=unused50=
unused51=unused52=unused53=unused54=unused55=unused56=unused57=unused58=unused59=unused60=
unused61=unused62=unused63=unused64=unused65=unused66=unused67=unused68=unused69=unused70=
unused71=unused72=unused73=unused74=unused75=unused76=unused77=unused78=unused79=unused80=
unused81=unused82=unused83=unused84=unused85=unused86=unused87=unused88=unused89=unused90=
unused91=unused92=unused93=unused94=unused95=unused96=unused97=unused98=unused99=unused100 = 0;
}
public static void main(String[] args) {
try{
stackLeak();
}catch (Error e){
System.out.println("stack length : " + stackLength);
throw e;
}
}
}
public class JavaVMStackSOF{
private void dontStop(){
while(true){
}
}
private void stackLeakByThread(){
while (true){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
dontStop();
}
});
thread.start();
}
}
public static void main(String[] args) {
JavaVMStackSOF oom = new JavaVMStackSOF();
oom.stackLeakByThread();
}
}
import java.util.HashSet;
import java.util.Set;
public class RuntimeConstantPoolOOM {
public static void main(String[] args) {
//使用Set保持常量池的引用,避免Full GC回收常量池的行为
Set<String> set = new HashSet<>();
//在short范围内足以让6MB的PermSize产生OOM了
short i = 0;
while(true){
set.add(String.valueOf(i++).intern());
}
}
}
public class RuntimeConstantPoolOOM {
public static void main(String[] args) {
String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1);
String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern() == str2);
}
}
通过生成大量的动态类可以,当前的很多主流框架,如Spring、Hibernate对类进行增强的时候,都会使用到CGLib这类字节码技术
- 当增强的类越多,就需要越大的方法区以保证动态生成的新类型可以载入内存。
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class DirectMemoryOOM {
private static final int _1MB = 1024 * 1024;
public static void main(String[] args) {
Field unsafeField = Unsafe.class.getDeclaredField()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
while(true){
unsafe.allocateMemory(_1MB);
}
}
}