What's the difference between StackOverflowError and OutOfMemoryError

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

    In Java Virtual Machine there are several memory area defined :

    1. Java Virtual Machine stacks
    2. Heap area
    3. Method area
    4. Run time constant pool
    5. Native method stacks

    In all above, you can choose your precision that memory allocated to those memory area will be fixed or will be changed dynamically at runtime.

    Now about the question, OutOfMemoryError is applicable for all of the above listed. OutOfMemoryError will be thrown if memory expansion of any of the memory area will be attempted but enough memory is not available to allocate.

    and StackOverFlowError is applicable for Native Method Stack and Java Virtual Machine Stack. StackOverFlowError will be thrown If the computation in a thread requires a larger stack than is permitted.

    For Detailed reference you can read THE STRUCTURE OF THE JAVA VIRTUAL MACHINE

提交回复
热议问题