What is a StackOverflowError in Java?
A StackOverflowError is simply signals that there is no more memory available. It extends the VirtualMachineError class, which indicates that the JVM (Java Virtual Machine) is broken, or it has run out of resources and cannot operate.
If you have a function like:
In the above code myFunction() will keep calling itself, getting deeper and deeper, and when the space used to keep track of what functions you're in is filled up, you get the stackoverflow error . The common cause for a stack overflow is a bad recursive call. Typically, this is caused when your recursive functions doesn't have the correct termination condition, so it ends up calling itself forever.
Reason for StackOverflowError
Parameters and local variables are allocated on the stack . The stack typically lives at the upper end of your address space and as it is used up it heads towards the bottom of the address space. Your process also has a heap, which lives at the bottom end of your process. As you allocate memory, this heap can grow towards the upper end of your address space. As you can see, there is a potential for the heap to "collide" with the stack. If there is no space for a new stack frame then, the StackOverflowError is thrown by the Java Virtual Machine (JVM).
- If the stack is full you can't push, if you do you'll get stack overflow error.
- If the stack is empty you can't pop, if you do you'll get stack underflow error.
What is a stacktrace?
A stacktrace is a very helpful debugging tool. It is a list of the method calls that the application was in the middle of when an Exception was thrown. This is very useful because it doesn't only show you where the error happened, but also how the program ended up in that place of the code.
More about.... stacktrace in java