Sunday, 6 February 2011

Java: OutOfMemoryError vs StackOverflowError

Some Java fundamentals that I read recently. Yes a bit ashamed ( : (  ) of myself that I never really considered their differences apart from knowing that they both relates to a lack of memory. But essentially the cause of their problems are actually quite different! : O


OutOfMemoryError:
Thrown when Java is running out of memory and cannot allocate any more memory in the heap for the object

StackOverflowError:
The stack space lives at the top-end of the address space and the heap lives at the bottom-end of the address space. As stack grows, it grows downwards. As heap grows, it grows upwards. So there is a potential that the two can collide and when that happens, you get a StackOverflowError thrown. Typically happens when you call some methods recursively (that keeps going-and-going).

(Really good explanation on StackOverflowError here: http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error)

2 comments:

  1. Simple and good Explanation !!! Keep Posting

    ReplyDelete
  2. Good Explanation,
    checkout the video as well
    https://www.youtube.com/watch?v=0YebJQSZE_E

    ReplyDelete