What's the difference between StackOverflowError and OutOfMemoryError

后端 未结 11 1500
不知归路
不知归路 2020-12-04 09:07

What\'s the difference between StackOverflowError and OutOfMemoryError and how to avoid them in application?

11条回答
  •  我在风中等你
    2020-12-04 09:25

    Short answer:

    • OutOfMemoryError is related to Heap.
    • StackOverflowError is related to stack

    Long answer:

    When you start JVM you define how much RAM it can use for processing. JVM divides this into certain memory locations for its processing purpose, two of those are Stack & Heap

    If you have large objects (or) referenced objects in memory, then you will see OutofMemoryError. If you have strong references to objects, then GC can't clean the memory space allocated for that object. When JVM tries to allocate memory for new object and not enough space available it throws OutofMemoryError because it can't allocate the required amount of memory.

    How to avoid: Make sure un-necessary objects are available for GC

    All your local variables and methods calls related data will be on the stack. For every method call, one stack frame will be created and local as well as method call related data will be placed inside the stack frame. Once method execution is completed, the stack frame will be removed. ONE WAY to reproduce this is, have an infinite loop for method call, you will see stackoverflow error, because stack frame will be populated with method data for every call but it won't be freed (removed).

    How to avoid: Make sure method calls are ending (not in an infinite loop)

提交回复
热议问题