専門家の理解が必要です
このプログラムはブロックをキャッチしません(ヒープがいっぱいなので、理由を理解したいです)
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 に対して異なる結果が表示された後、少し混乱しています。ガイドしてください