I need some understanding from you experts
This Program does not go to catch block(as Heap is full, but I want to understand why)
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"
But the below Program runs good, even after getting 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");
}
}
}
}
I am bit confused, after seeing different results for the same error OOME. Please guide
Most mammals are born knowing how to find their mother's nipples and drink, and would quickly perish if they didn't -- making suckling an example of an instinctive or "innate" behavior hard-wired into the brain.
Two types of innate behavior that describe how an organism moves are taxis and kinesis.
Repetitive, purposeless behaviors are a common symptom of autism. 1 Such behaviors might include repetitively lining up toys, spinning objects, or opening and closing drawers or doors. Repetitive behaviors can also involve talking or asking about the same thing over and over again.
Stimuli may include the sight of food, the sound of a potential predator, or the smell of a mate. They may also include such daily events as nightfall and seasonal events such as decreasing temperatures. Animals respond to stimuli. Each of these stimuli elicits specific behaviors from animals.
My understanding is that, in the first program, the OOME is thrown when trying to allocate a new Long object, which takes only a few bytes of memory. This means that the heap is completely full, and that no memory is available anymore to run the catch block.
In the second program, the OOME is thrown when trying to allocate a new array of 1 million bytes. It fails, but that can mean that the heap still has 999,990 bytes available, which is enough to let the catch block execute.
Both programs enter the catch block. What happens is that either of them can run out of memory again before it manages to generate and print a message, and the program receives a new OutOfMemoryError
right away.
When I try the first program I do see the output that the catch block generates, which goes to show how unpredictable program execution is once an error is thrown. In the stack trace I see that the OOME was thrown while trying to create a new backing array for the ArrayList
, which means there still was enough memory to run the catch block. If the OOME was thrown while creating the smaller Long
object things might be different.
What's important to note is that you can't really catch an OutOfMemoryError
in a meaningful way: the error can be thrown in a very inconvenient place, leaving internal data structures in inconsistent states. In this case there are no visible ill-effects, but if you investigated the internal fields of the ArrayList
you would find that its modCount
field has been incremented without the list actually having been modified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With