oome java_java – 为什么OOME尝试捕获它时会有不同的行为?

我需要你们专家的一些了解

本程序不会阻止阻塞(因为Heap已满,但我想了解原因)

public class OOME_NotCatch {

static List l = new ArrayList();

static Long i = new Long(1);

public static void main(String[] args) {

try {

while (true) {

l.add(i);

i++;

}

} catch (OutOfMemoryError e) {

e.printStackTrace();

System.out.println("Encountered OutOfMemoryError");

}

}

}

//Console : Exception in thread "main"

但即使在获得OOME之后,下面的程序运行良好:

public class Catch_OOME_Collection {

static List l = new ArrayList();

public static void main(String[] args) {

try {

while (true) {

l.add(new byte[1000000]);

System.out.println("size " + l.size());

}

} catch (OutOfMemoryError e) {

System.out.println("Encountered OutOfMemoryError");

e.printStackTrace();

System.out.println("size of list is " + l.size());

Iterator i = l.iterator();

while(i.hasNext()){

System.out.println(i.next().toString());

i.remove();

}

while (true) {

System.out.println("keep printing");

}

}

}

}

在看到同样错误OOME的不同结果后,我有点困惑.请指导

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值