What's the difference between StackOverflowError and OutOfMemoryError

后端 未结 11 1481
不知归路
不知归路 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:44

    StackOverflowError

    • It is related to Stack memory.
    • It occurs when Stack is full.
    • It is thrown when you call a method and there is no space left in the stack.
    • It occurs when you are calling a method recursively without proper terminating condition.
    • How to avoid? Make sure that methods are finishing their execution and leaving the stack memory.

    OutOfMemoryError

    • It is related to heap memory.
    • It occurs when heap is full.
    • It is thrown when you create a new object and there is no space left in the heap.
    • It occurs when you are creating lots of objects in the heap memory.
    • How to avoid? Try to remove references to objects which you don’t need anymore.

提交回复
热议问题