-2

I read that both OutOfMemoryError and StackOverFlowError can happen in stack. But I wonder when will OOM be thrown in stack?

Is it thrown when creating new object in stack? Then I guess this new object does not belong to a stack frame, otherwise SOF will be thrown.

Thank you very much for answering. I searched on google but did not find a related answer.

I'm very sorry for previous unclear abreviations!!

Tokic
  • 3
  • 3
  • 3
    https://stackoverflow.com/questions/11435613/whats-the-difference-between-stackoverflowerror-and-outofmemoryerror/11435639 – HUTUTU Feb 09 '22 at 06:36
  • 1
    Please provide a link to where you read that. – tgdavies Feb 09 '22 at 06:51
  • Does this answer your question? [What's the difference between StackOverflowError and OutOfMemoryError](https://stackoverflow.com/questions/11435613/whats-the-difference-between-stackoverflowerror-and-outofmemoryerror) – Brian Tompsett - 汤莱恩 Feb 09 '22 at 13:20

2 Answers2

1

I strongly recommend to reduce the unnecessary use of abbreviation. While it’s clear that “OOM” refers to OutOfMemoryError, the meaning of “EOF” and “SOF” is not. “EOF” normally refers to “end of file” which does not apply here. “SOF” could refer to StackOverflow, but that’s rather unusual. I don’t think that writing the full names of the errors would take too much of your lifetime.

Regarding OutOfMemoryError, the answer is that it is implementation dependent.

The JVM specification, §2.5.2. Java Virtual Machine Stacks, says

The following exceptional conditions are associated with Java Virtual Machine stacks:

  • If the computation in a thread requires a larger Java Virtual Machine stack than is permitted, the Java Virtual Machine throws a StackOverflowError.
  • If Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

So in principle, an OutOfMemoryError can be thrown under certain circumstances, but the widespread HotSpot JVM doesn’t support dynamic stack expansion so this will never be a cause for an OutOfMemoryError.

The creation of a new Thread may fail with an OutOfMemoryError, but since there are several allocations involved, including the Thread instance on the heap, it hardly ever matters whether the failed allocation was supposed to provide the stack memory of the new thread or serve some other purpose.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • Thank you very much for answering my question! And I am sorry again for using abreviation improperly. – Tokic Feb 11 '22 at 06:32
0

I believe you are talking about OutOfMemoryError and StackOverflowError.

  • OutOfMemoryError happens when there is no more free space in the Heap for allocating a new Object. The rest of the space is occupied by strongly referenced objects. Garbage collection cannot reclaim the space those objects space. Objects and arrays are always allocated in the Heap, not in the Stack. (Scape Analysis/Scalar replacement/JIT may be an exception)

  • StackOverFlow happens when there is no more free space in the jvm stack. The stack stores local method variables and frames, which are created every method invocation (the usual error stacktrace for instance).

You can read further details about Heap and Stack in the JVM specification.

usuario
  • 2,132
  • 1
  • 10
  • 26