-2

I mean when a program recurses infinitely and the computer runs out of memory does the java compiler throw a stack over flow error or out of memory error?

I am pretty sure its out of memory, but It has been a long night with little sleep could someone clarify this for me?

Hoon
  • 395
  • 1
  • 4
  • 8
  • 1
    http://stackoverflow.com/questions/4959172/java-root-cause-java-lang-outofmemoryerror-error and http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error – PSR Mar 11 '13 at 19:41
  • I think it dependence on where you want to allocate memory, on stack (that means local variables) (or) on heap? If it is due to stack full because of recursion or something else, it would be StackOverflow, otherwise OutOfMemory. – kosa Mar 11 '13 at 19:41
  • the test seems fairly trivial. By setting the JVMs memory to a small value you can test this without breaking your development OS. – Paul Rubel Mar 11 '13 at 19:41
  • Why not test it for yourself with an infinite recursive function call? – Adel Boutros Mar 11 '13 at 19:41
  • Java compiler will not throw any `Exception`. That will happen in runtime. – partlov Mar 11 '13 at 19:42
  • `does the java compiler throw... ` The Java compiler doesn't throw the error, that happens at runtime – leonbloy Mar 11 '13 at 19:43
  • `OutOfMemoryError` is related to a failed memory allocation. The specs say it is thrown when the JVM cannot allocate an object because it is out of memory, and garbage collection couldn't free more. `StackOverflowError` is related to recursion (when an app recurses too deeply, by the specs). A Stack Overflow happens when your stacks grows big and it intercepts with your assigned heap. They both have delimited different areas allocated for use, being the Stack "up" and the heap "below". When the stack grows too much, it overlaps with the heap thus causing this error. – Fritz Mar 11 '13 at 19:47

1 Answers1

1

Typically you'd get a stack overflow exception.

That said, it can be either, depending whether it's the stack memory or the heap memory that gets exhausted first. That in turn depends on how much stack/heap memory is required by each recursive call, and how much is available (heap size and stack size can be configured independently of one another).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • http://stackoverflow.com/questions/11435613/whats-the-difference-between-stackoverflowerror-and-outofmemoryerror/29563736#29563736 – Raúl Apr 10 '15 at 14:17