Search code examples
jquerystack-overflow

How to create a stackoverflow in jQuery, and ways to fix it


I've gotten the StackOverflow message when my computer runs out of memory. I was wondering if just for fun you can recreate these errors, and what is the best way to solve this problem.


Solution

  • Stack overflows are usually caused by a recursion loop, where a function attempts to call it's self over and over.

    The overflow occurs because the browser cannot allocate enough memory to handle the growing stack of data. Each time the function recurs the return is added onto the stack.

    var overflow = function(){
      return overflow();
    };
    
    overflow();
    

    As for fixing them, just be careful with recursion.

    Wikipedia has a short article that explains it well: http://en.wikipedia.org/wiki/Stack_overflow