Node.js application running out of stack memory

Viewed 74

I want to create a bot to connect to my node.js server and I am getting some strange crashes that all produce this error message: RangeError: Maximum call stack size exceeded Whats even stranger, is that this doesn't seam to be triggered by any specific command at all, since this code alone:

function loop() {
  loop();
}

loop();

produces the crash. I have looked at my memory usage and it is nowhere near the limit. What is wrong with this code?

1 Answers

Calling a function itself allocates some space on the system stack, and returning from that function frees that space*. If the function calls itself again and again without returning, it's going to, sooner or later, run out of stack memory.
If you want an infinite loop that doesn't run out of memory, you can write something like while (true);.

*In high-level languages, it does so automatically. In assembly language, not exactly, you need to do that manually, you need to do stuff like this (this is from permutations.aec file from this ZIP):

                fld dword [subscript]
                fistp dword [subscript]
                mov ebx,[subscript]
                pushIntegerToTheSystemStack (countDigits+ebx)
                pushStringToTheSystemStack integerSign
                call [printf]
                add esp,8 ;Cleaning up the system stack after "printf".

As I approached assembly knowing only high-level languages (such as JavaScript), that behaviour was confusing to me at first and caused my app to crash on large inputs before I figured out what's going on.

Related