Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Stack limitation in C# [duplicate]

i wonder how much calls we can perform in stack in c# before we get stack overflow exception

so i decided to write the following code

    static void Method2(int Calls)
    {
        if(!Calls.Equals(0))
            Method1(--Calls);//if more calls remain call method1 and reduce counter
    }
    static void Method1(int Calls)
    {
        if (!Calls.Equals(0))//if more calls remain call method2 and reduce counter
            Method2(--Calls);

    }
    static void Main(string[] args)
    {
        var Calls= 42994;//number of calls(stack overflow appears for large number)
        Method1(Calls);
    }

my question is how compiler decides to throw stack overflow exception is this about memory limitations? once i put 42995 i got stackoverflow but this number is not constant so how this works?

like image 641
M.kazem Akhgary Avatar asked Apr 23 '15 19:04

M.kazem Akhgary


People also ask

What is the limit of a call stack?

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.

Why is the stack size limited?

This is because stack mostly gets involved into multi-threaded programming and if systems allows stack to grow whenever required then it will be difficult to track each thread at real time cause multi-threaded programs need separate stack for each thread.

What is the size of stack in C?

Stacks are temporary memory address spaces used to hold arguments and automatic variables over subprogram invocations. The default size of the main stack is about eight megabytes.

Is stack limited in size?

Thus, the only real limitation on stack size is available memory on 64 bit systems (address space fragmentation is rather theoretical with a 16 zebibyte address space size). That is the stack for the first thread only. New threads have to allocate new stacks and are limited because they will run into other objects.


1 Answers

Each thread has a stack size. The predefined stack size for the main thread of a program is fixed in the exe file. Each recursive call you make, you consume a little of this stack. When you finish it, the CLR throws a StackOverflowException. For Console/Graphical programs the default stack size should be 1mb of memory. You can't make this memory "bigger" from inside the program (you can use editbin.exe to change it from "outside" the program). This memory isn't dynamic. It is fixed (technically, the address space reserved for this memory is fixed, the memory is really allocated by the Windows OS on demand, probably 4kb at a time, but always up to the reserved address space). You can create secondary threads with the stack size you want.

Note that the handling of the stack in this way is a limitation of x86/x64 architecture, http://en.wikipedia.org/wiki/Stack-based_memory_allocation:

Some processors families, such as the x86, have special instructions for manipulating the stack of the currently executing thread. Other processor families, including PowerPC and MIPS, do not have explicit stack support, but instead rely on convention and delegate stack management to the operating system's application binary interface (ABI).

like image 199
xanatos Avatar answered Nov 09 '22 17:11

xanatos