Java / JVM Architecture (Java21) Interview questions
What is the difference between StackOverflowError and OutOfMemoryError?
Both are serious runtime failures, but they come from exhausting different memory areas for different reasons.
| StackOverflowError | OutOfMemoryError |
| The per-thread JVM Stack runs out of space | Heap, Metaspace, or native memory runs out of space |
| Usual cause: deep or infinite recursion | Usual causes: memory leaks, undersized heap, too many threads, classloader leaks |
| Affects only the one thread that overflowed | Can affect the whole JVM process, depending on the area exhausted |
OutOfMemoryError also comes with sub-messages that point to the specific cause: Java heap space for an exhausted heap, Metaspace for exhausted class metadata, GC overhead limit exceeded when GC runs constantly but reclaims almost nothing, and unable to create new native thread when OS thread or native memory limits are hit.
More Related questions...