Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion - Call stack fails to pop when testing the maximum stack size

Basically call stack will start to pop out the function calls one by one when the last-in function call returns. But when ever I try to create a call stack nearer to the size of its maximum, An uncaught expression is getting raised.

//Code for testing the stack size
var cnt = 0;

function test(){
 //Max stack size is nearer to ~41800
 if(cnt++ == 41763){
  console.log('finished');
  return true;
 }
 return test();
}

test();

So the above code is throwing an exception for me in chromium Version 49.0.2623.112 m like below,

Uncaught exception

< true

Please not that the above error has no message in it. My question here is,

The last function call in the stack has returned true that means the stack size was not exceeded. why didn't the other function calls in that stack was not returned? And what is the reason for this blank exception message?

like image 973
Rajaprabhu Aravindasamy Avatar asked Apr 26 '16 16:04

Rajaprabhu Aravindasamy


People also ask

How do I fix maximum call stack size exceeded error?

The call stack is limited in size, and when it's exceeded, the RangeError is thrown. This can happen when a deeply nested function is called, or when a lot of new variables are created. The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created.

What is the maximum call stack size?

Without any local variables, each function call takes up 48 bytes during the execution, and you are limited to less than 1MB for all local function frames. Each boolean and number variable takes 8 bytes of memory.

What is the maximum call stack size Nodejs?

queue, queue. Show activity on this post. Regarding increasing the max stack size, on 32 bit and 64 bit machines V8's memory allocation defaults are, respectively, 700 MB and 1400 MB.

Which of the given code snippet leads to maximum call stack size exceeded error?

The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.


1 Answers

The problem here is

console.log('finished');

this adds some extra functions to the call stack that will let the limit exceed, but through exception handling your code gets executed anyway.

Try to run it without console.log and you see you get to the limit and see either a true or an exception.

like image 56
overflowed Avatar answered Sep 30 '22 17:09

overflowed