我需要一些专家的理解
该程序不去捕获块(因为堆已满,但我想了解原因)
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 的不同结果后,我有点困惑。请指导