What is a StackOverflowError?

745,442

Solution 1

Parameters and local variables are allocated on the stack (with reference types, the object lives on the heap and a variable in the stack references that object on the heap). 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 (i.e. towards zero).

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 (a bit like tectonic plates!!!).

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. Or when the termination condition is fine, it can be caused by requiring too many recursive calls before fulfilling it.

However, with GUI programming, it's possible to generate indirect recursion. For example, your app may be handling paint messages, and, whilst processing them, it may call a function that causes the system to send another paint message. Here you've not explicitly called yourself, but the OS/VM has done it for you.

To deal with them, you'll need to examine your code. If you've got functions that call themselves then check that you've got a terminating condition. If you have, then check that when calling the function you have at least modified one of the arguments, otherwise there'll be no visible change for the recursively called function and the terminating condition is useless. Also mind that your stack space can run out of memory before reaching a valid terminating condition, thus make sure your method can handle input values requiring more recursive calls.

If you've got no obvious recursive functions then check to see if you're calling any library functions that indirectly will cause your function to be called (like the implicit case above).

Solution 2

To describe this, first let us understand how local variables and objects are stored.

Local variable are stored on the stack:

Enter image description here

If you looked at the image you should be able to understand how things are working.

When a function call is invoked by a Java application, a stack frame is allocated on the call stack. The stack frame contains the parameters of the invoked method, its local parameters, and the return address of the method. The return address denotes the execution point from which, the program execution shall continue after the invoked method returns. If there is no space for a new stack frame then, the StackOverflowError is thrown by the Java Virtual Machine (JVM).

The most common case that can possibly exhaust a Java application’s stack is recursion. In recursion, a method invokes itself during its execution. Recursion is considered as a powerful general-purpose programming technique, but it must be used with caution, to avoid StackOverflowError.

An example of throwing a StackOverflowError is shown below:

StackOverflowErrorExample.java:

public class StackOverflowErrorExample {

    public static void recursivePrint(int num) {
        System.out.println("Number: " + num);
        if (num == 0)
            return;
        else
            recursivePrint(++num);
        }

    public static void main(String[] args) {
        StackOverflowErrorExample.recursivePrint(1);
    }
}

In this example, we define a recursive method, called recursivePrint that prints an integer and then, calls itself, with the next successive integer as an argument. The recursion ends until we pass in 0 as a parameter. However, in our example, we passed in the parameter from 1 and its increasing followers, consequently, the recursion will never terminate.

A sample execution, using the -Xss1M flag that specifies the size of the thread stack to equal to 1 MB, is shown below:

Number: 1
Number: 2
Number: 3
...
Number: 6262
Number: 6263
Number: 6264
Number: 6265
Number: 6266
Exception in thread "main" java.lang.StackOverflowError
        at java.io.PrintStream.write(PrintStream.java:480)
        at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
        at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
        at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
        at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
        at java.io.PrintStream.write(PrintStream.java:527)
        at java.io.PrintStream.print(PrintStream.java:669)
        at java.io.PrintStream.println(PrintStream.java:806)
        at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:4)
        at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:9)
        at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:9)
        at StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:9)
        ...

Depending on the JVM’s initial configuration, the results may differ, but eventually the StackOverflowError shall be thrown. This example is a very good example of how recursion can cause problems, if not implemented with caution.

How to deal with the StackOverflowError

  1. The simplest solution is to carefully inspect the stack trace and detect the repeating pattern of line numbers. These line numbers indicate the code being recursively called. Once you detect these lines, you must carefully inspect your code and understand why the recursion never terminates.

  2. If you have verified that the recursion is implemented correctly, you can increase the stack’s size, in order to allow a larger number of invocations. Depending on the Java Virtual Machine (JVM) installed, the default thread stack size may equal to either 512 KB, or 1 MB. You can increase the thread stack size using the -Xss flag. This flag can be specified either via the project’s configuration, or via the command line. The format of the -Xss argument is: -Xss<size>[g|G|m|M|k|K]

Solution 3

If you have a function like:

int foo()
{
    // more stuff
    foo();
}

Then foo() 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 stack overflow error.

Solution 4

Stack overflow means exactly that: a stack overflows. Usually there's a one stack in the program that contains local-scope variables and addresses where to return when execution of a routine ends. That stack tends to be a fixed memory range somewhere in the memory, therefore it's limited how much it can contain values.

If the stack is empty you can't pop, if you do you'll get stack underflow error.

If the stack is full you can't push, if you do you'll get stack overflow error.

So stack overflow appears where you allocate too much into the stack. For instance, in the mentioned recursion.

Some implementations optimize out some forms of recursions. Tail recursion in particular. Tail recursive routines are form of routines where the recursive call appears as a final thing what the routine does. Such routine call gets simply reduced into a jump.

Some implementations go so far as implement their own stacks for recursion, therefore they allow the recursion to continue until the system runs out of memory.

Easiest thing you could try would be to increase your stack size if you can. If you can't do that though, the second best thing would be to look whether there's something that clearly causes the stack overflow. Try it by printing something before and after the call into routine. This helps you to find out the failing routine.

Solution 5

A stack overflow is usually called by nesting function calls too deeply (especially easy when using recursion, i.e. a function that calls itself) or allocating a large amount of memory on the stack where using the heap would be more appropriate.

Share:
745,442
user2918201
Author by

user2918201

In August of 2008 I became curious about Ruby and took up programming as a hobby. Since then I have become curious about a lot of other things, and my aspirations have quickly expanded. I very much enjoy programming, and the open source phenomenon strikes me as a window into a sustainable future. I hope that in a few years I will be able to join the scene and contribute to that future. My dreams and ambitions have been multiplied! In 2009 I decided that I would, at the age of 26, return to university and get a degree in computer science. To that end I began studying math, something I had never done before. In summer of 2010 I enrolled in a college and began taking first year computer science courses. By this time I was completely roped in by the beauty of programming. At the time I hoped to be able to transfer to a university by summer of 2011 and receive my first degree in 2014. Rather than transferring to a university, though, I took on an internship. A friend I had met through Elysian coffee, my part time job, offered to take me on as his intern in the spring of 2011 while I was taking courses. This internship turned full time during the summer, and I worked on a number of Rails and Mac projects, mostly in ruby. My love of Ruby increased tenfold, as did my understanding of the programmer's ecosystem. University transfer was pushed back to the summer of 2012, but my goal was still to graduate in 2014. Having worked with a freelance programmer for some time, I decided that after graduating, but before entering the workforce, I would dedicate some time to personal projects. At about the time these thoughts occurred to me I became aware that Canada had established a working holiday visa treaty with Taiwan. I resolved to take advantage of this unique opportunity, and to spend the end of 2014 (the year I would turn 30) and the beginning of 2015 in Taiwan as a master-less programmer. Ultimately I would like to use computer science to help "save the world". Once I have learned enough, I hope to be able to dedicate myself to whatever desperately needs doing. Whether that be related to energy, or poverty, or space travel, I want to be there with those people computing what they need computed.

Updated on October 05, 2021

Comments

  • user2918201
    user2918201 over 2 years

    What is a StackOverflowError, what causes it, and how should I deal with them?

  • Greg
    Greg over 15 years
    Oops, didn't see the Java tag
  • user2918201
    user2918201 over 15 years
    Also, from the original poster here: nesting functions too deeply in what? Other functions? And: how does one allocate memory to the stack or heap (since, you know, I've clearly done one of these things without knowing).
  • user2918201
    user2918201 over 15 years
    I would totally like to add code, but as I don't know what causes stack overflows I am not sure what code to add. adding all the code would be lame, no?
  • user2918201
    user2918201 over 15 years
    Original poster: hey this is great. So recursion is always responsible for stack overflows? Or can other things be responsible for them as well? Unfortunately I am using a library... but not one that I understand.
  • C. K. Young
    C. K. Young over 15 years
    @Ziggy: Yes, if one function calls another function, which calls yet another function, and so on, after many levels, your program will have a stack overflow. [continues]
  • C. K. Young
    C. K. Young over 15 years
    [...continued] In Java, you can't directly allocate memory from the stack (whereas in C, you can, and this would then be something to watch for), so that's unlikely to be the cause. In Java, all direct allocations come from the heap, by using "new".
  • Cheery
    Cheery over 15 years
    Wrong. Your function is tail-recursive. Most compiled languages have tail-recursion optimizations. This means the recursion reduces into a simple loop and you will never hit stack overflow with this piece of code on some systems.
  • C. K. Young
    C. K. Young over 15 years
    Is your project open-source? If so, just make a Sourceforge or github account, and upload all your code there. :-)
  • user2918201
    user2918201 over 15 years
    this sounds like a great idea, but I am such a noob that I don't even know what I would have to upload. Like, the library that I am importing classes that I am extending etc... are all unknowns to me. Oh man: bad times.
  • user2918201
    user2918201 over 15 years
    Ha ha ha, so here it is: while (points < 100) {addMouseListeners(); moveball(); checkforcollision(); pause(speed);} Wow do I feel lame for not realizing that I would end up with a stackfull of mouse listeners... Thanks guys!
  • JB King
    JB King over 15 years
    No, stack overflows can also come from variables being too big to allocate on the stack if you look up the Wikipedia article on it at en.wikipedia.org/wiki/Stack_overflow .
  • horseyguy
    horseyguy over 14 years
    Cheery, which non-functional languages support tail recursion?
  • Pacerier
    Pacerier over 12 years
    @banister and some implementations of javascript
  • Pacerier
    Pacerier over 12 years
    @ChrisJester-Young Isn't it true that if I have 100 local variables in a method, all of it goes on the stack without exceptions?
  • Pacerier
    Pacerier over 12 years
    Is there such a thing as a stack underflow ?
  • C. K. Young
    C. K. Young over 12 years
    @Pacerier: The local variables are all on the stack, correct. However, if the variables are of reference type, the objects they point to are all allocated on the heap (except cases where escape analysis optimisation kicks in). (NB: In Java, arrays are of a reference type, even arrays of primitives.)
  • Pacerier
    Pacerier over 12 years
    @ChrisJester-Young Btw I was wondering is there any chance that a variable on the stack could be seen by other threads?
  • C. K. Young
    C. K. Young over 12 years
    @Pacerier: The variable itself? No (stacks are thread-local). The objects pointed to by reference variables? Yes.
  • Pacerier
    Pacerier over 12 years
    @ChrisJester-Young sry I think I've phrased it wrongly. I actually meant "is there any chance a stack could be a non-thread-local stack" ?
  • C. K. Young
    C. K. Young over 12 years
    @Pacerier: A non-thread-local stack doesn't make any sense: two threads would then scribble on each other's call frames!
  • Pacerier
    Pacerier about 12 years
    @ChrisJester-Young Do you mean that primitive variables that are shared across threads are allocated on the heap?
  • Score_Under
    Score_Under about 11 years
    A stack underflow is possible in assembly (popping more than you pushed), though in compiled languages it'd be near impossible. I'm not sure, you might be able to find an implementation of C's alloca() which "supports" negative sizes.
  • kevin
    kevin over 10 years
    I think with JVM, it doesn't actually matter what the spec your laptop is.
  • Hot Licks
    Hot Licks over 10 years
    It should be pointed out that it's almost impossible to "handle" a stack overflow error. In most environments, to handle the error one needs to run code on the stack, which is difficult if there is no more stack space.
  • jcsahnwaldt Reinstate Monica
    jcsahnwaldt Reinstate Monica over 9 years
    Java keeps heap and stack in different memory areas. (Most JVMs do, anyway.) They don't collide, the stack simply has a limited size. (Can be set with -Xss at program start.) Otherwise, the explanation is fine.
  • jcsahnwaldt Reinstate Monica
    jcsahnwaldt Reinstate Monica over 9 years
    @JB King: Doesn't really apply to Java, where only primitive types and references are kept on the stack. All the big stuff (arrays and objects) is on the heap.
  • Koray Tugay
    Koray Tugay almost 9 years
    Stack overflow means exactly that: a stack overflows. Usually there's a one stack in the program that contains local-scope variables -> No, every thread has its own stack which contains stack frames for every method invocation that contains local variables..
  • user207421
    user207421 about 8 years
    @HotLicks Code doesn't run on the stack.
  • Hot Licks
    Hot Licks about 8 years
    @EJP - I could have phrased that a little better. In most environments, to handle the error one needs to run code which utilizes the stack for call/return and variable storage -- difficult if there is no more stack space.
  • goerlibe
    goerlibe almost 7 years
    There seems to be a bug in some java versions when using windows where the -Xss argument only takes effect on new threads
  • AKs
    AKs over 6 years
    @horseyguy Scala has support for Tail recursion.
  • Admin
    Admin over 6 years
    This captures the essence of what can create a stack overflow. Nice.
  • Keale
    Keale almost 6 years
    @Sean, I have approved an edit proposal to your answer, and added a minor clarification myself. I'm not a native speaker, though I'm fairly confident in my English, and this clarification is how I understood that particular sentence. If I misunderstood then please feel free to rollback :) Cheers~