----------------------------------------------------------------------------------------------------------------
1.这段程序长期运行会有什么结果? (以为运行久了会OOM 但是不会) (没有配置-Xms -Xmx 默认)
程序中第一次new的对象在第二次new的时候失去引用
( 垃圾回收 回收到根节点没有引用路径的对象)
----------------------------------------------------------------------------------------------------------------
for (int i = 0; i < 290; i++) {
Map map = new HashMap();
}
System.out.println("do ="+ ++a);
/*try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
----------------------------------------------------------------------------------------------------------------
堆内存大小 128-256 程序运行ok
堆内存大小 50-50 程序运行ok
堆内存大小 10-20 程序运行ok
堆内存大小 5-5 程序运行出问题:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.test.alljava.HelloWorld.main(HelloWorld.java:20)
----------------------------------------------------------------------------------------------------------------
<span style="white-space:pre"> </span>List list= new ArrayList();
while(true){
for (int i = 0; i < 50; i++) {
Map map = new HashMap();
list.add(map);
}
System.out.println("do ="+ ++a+"~~~~~~~~~"+list.size());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
没有GC,直线上涨
最终导致:
堆内存大小 10-20 程序运行ok