2

I have been struggling with understanding these 2 java errors. I have read Java docs and some other stack overflow question threads and now I know the basics about these 2 errors. But I have 2 doubts that I have not been able to confirm yet.

  1. Is the below statement correct or wrong - ?

StackOverflowError affects a particular Thread of execution where as OutOfMemoryError affects the entire JVM

  1. Can StackOverflowError cause OutOfMemoryError to occur? If yes, then in what kind of scenario?

Really appreciate any help here.

Thank you.

Chesser
  • 455
  • 3
  • 9
  • My feeling is that the first statement is not correct. You'll get out of memory exception when the executable has exhausted allocated memory. Other executables should be unaffected, so that doesn't seem JVM-wide – baileyrt Jan 08 '19 at 02:18
  • 1. Incomplete. `StackOverflowError` also affects the entire JVM. 2. Unlikely. Generally speaking the stack is quite small, while the heap is large. What actual problem are you trying to solve? – Elliott Frisch Jan 08 '19 at 02:22
  • @ElliottFrisch - Thank you for your inputs. Working with a packaged application, seeing a lot of `StackOverflowError` because of infinite loops and some `OutOfMemoryError` here and there. Not able to figure out if they are related or not. – Chesser Jan 08 '19 at 10:51
  • @ElliottFrisch That’s implementation dependent. A JVM could have automatic stack expansion and in this case, insufficient memory for expanding a deep stack may cause an `OutOfMemoryError`. – Holger Jan 08 '19 at 14:58
  • @Holger I said it was "Unlikely", are you aware of a mainstream JVM that has automatic stack expansion? Heck, are you aware of a mainstream JVM that includes a [tail-call optimization](https://softwareengineering.stackexchange.com/a/272086/108326)? Because I ***want*** one. – Elliott Frisch Jan 08 '19 at 15:14
  • @ElliottFrisch the term “mainstream” implies that you knew it if it existed. The problem with tail-call optimization is not only that HotSpot doesn’t have it, but that you still couldn’t rely on it if present, as long as it is an optimization that only kicks in for hot spots according to a heuristic with unpredictable result. Compare with [Why is the max recursion depth I can reach non-deterministic?](https://stackoverflow.com/q/27043922/2711488). For algorithms with deep recursion, you don’t want a non-deterministic tail-call optimization. – Holger Jan 08 '19 at 15:43
  • @Holger Actually, the term "mainstream" allows me to follow-up now and ask if you know of any "non-mainstream" JVM implementation with either of the two features under discussion? – Elliott Frisch Jan 08 '19 at 15:47
  • @ElliottFrisch does my own not-yet-finished JVM count? – Holger Jan 08 '19 at 15:53
  • @Holger I don't think OP is running your unfinished magnum opus. But I've certainly been wrong before. – Elliott Frisch Jan 08 '19 at 16:07
  • @ElliottFrisch the OP didn’t say if they are interested in the behavior of a particular implementation or “the intricacies of formal or authoritative specifications” (quote from [tag:language-lawyer] tag). I usually prefer to provide both sides, unless the question has been closed already, of course. – Holger Jan 08 '19 at 16:13

0 Answers0