.NET stack size & recursion
According to here, the Windows x64 program should have default stack size 4MB. So I wrote a program to see how many levels of recursion can a typical function support before getting stack overflow.
Here's the program:
class Program
{
static void Main(string[] args)
{
Rec(0);
}
static void Rec(int x)
{
int y0 = x;
int y1 = y0 + x;
int y2 = y1 + x;
int y3 = y2 + x;
int y4 = y3 + x;
int y5 = y4 + x;
int y6 = y5 + x;
int y7 = y6 + x;
int y8 = y7 + x;
int y9 = y8 + x;
int y10 = y9 + x;
int y11 = y10 + x;
int y12 = y11 + x;
int y13 = y12 + x;
int y14 = y13 + x;
int y15 = y14 + x;
int y16 = y15 + x;
int y17 = y16 + x;
int y18 = y17 + x;
int y19 = y18 + x;
int y20 = y19 + x;
Console.WriteLine($"depth: {x}, local size: {22} >>>>>>>>>>>>>>> {y20}");
Rec(x + 1);
Console.WriteLine($"depth: {x}, local size: {22} <<<<<<<<<<<<<<< {y20}");
}
}
I configured it to build in release
and x64
. The output is:
depth: 17170, local size: 2 >>>>>>>>>>>>>>> 17170
depth: 16125, local size: 10 >>>>>>>>>>>>>>> 145125
depth: 16101, local size: 22 >>>>>>>>>>>>>>> 338121
That looks like the stack size is roughly 600KB. I took a dumpbin
, which
gives me below output. stack reverse
is 100000. 100KB? WTF? But basically,
a function with ~20 local variables + arguments can recurse 16,000 times.
Not a lot.
PE signature found
File Type: EXECUTABLE IMAGE
FILE HEADER VALUES
14C machine (x86)
3 number of sections
5A5F9A97 time date stamp Wed Jan 17 10:48:55 2018
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
22 characteristics
Executable
Application can handle large (>2GB) addresses
OPTIONAL HEADER VALUES
10B magic # (PE32)
48.00 linker version
A00 size of code
800 size of initialized data
0 size of uninitialized data
293E entry point (0040293E)
2000 base of code
4000 base of data
400000 image base (00400000 to 00407FFF)
2000 section alignment
200 file alignment
4.00 operating system version
0.00 image version
6.00 subsystem version
0 Win32 version
8000 size of image
200 size of headers
0 checksum
3 subsystem (Windows CUI)
8560 DLL characteristics
High Entropy Virtual Addresses
Dynamic base
NX compatible
No structured exception handler
Terminal Server Aware
100000 size of stack reserve
1000 size of stack commit
100000 size of heap reserve
1000 size of heap commit
0 loader flags
10 number of directories
0 [ 0] RVA [size] of Export Directory
28EC [ 4F] RVA [size] of Import Directory
4000 [ 5BC] RVA [size] of Resource Directory
0 [ 0] RVA [size] of Exception Directory
0 [ 0] RVA [size] of Certificates Directory
6000 [ C] RVA [size] of Base Relocation Directory
27B4 [ 1C] RVA [size] of Debug Directory
0 [ 0] RVA [size] of Architecture Directory
0 [ 0] RVA [size] of Global Pointer Directory
0 [ 0] RVA [size] of Thread Storage Directory
0 [ 0] RVA [size] of Load Configuration Directory
0 [ 0] RVA [size] of Bound Import Directory
2000 [ 8] RVA [size] of Import Address Table Directory
0 [ 0] RVA [size] of Delay Import Directory
2008 [ 48] RVA [size] of COM Descriptor Directory
0 [ 0] RVA [size] of Reserved Directory
SECTION HEADER #1
.text name
944 virtual size
2000 virtual address (00402000 to 00402943)
A00 size of raw data
200 file pointer to raw data (00000200 to 00000BFF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
60000020 flags
Code
Execute Read
RAW DATA #1
Debug Directories
Time Type Size RVA Pointer
-------- ------- -------- -------- --------
5A5F9A97 cv 11C 000027D0 9D0 Format: RSDS, {8C1B2824-CBBA-483B-B4E1-18BBA4A2FEAC}, 1, c:\users\ConsoleApp2\ConsoleApp2\obj\Release\ConsoleApp2.pdb
clr Header:
48 cb
2.05 runtime version
20E4 [ 6D0] RVA [size] of MetaData Directory
20003 flags
IL Only
32-Bit Required
32-Bit Preferred
6000001 entry point token
0 [ 0] RVA [size] of Resources Directory
0 [ 0] RVA [size] of StrongNameSignature Directory
0 [ 0] RVA [size] of CodeManagerTable Directory
0 [ 0] RVA [size] of VTableFixups Directory
0 [ 0] RVA [size] of ExportAddressTableJumps Directory
0 [ 0] RVA [size] of ManagedNativeHeader Directory
Section contains the following imports:
mscoree.dll
402000 Import Address Table
402914 Import Name Table
0 time date stamp
0 Index of first forwarder reference
0 _CorExeMain
SECTION HEADER #2
.rsrc name
5BC virtual size
4000 virtual address (00404000 to 004045BB)
600 size of raw data
C00 file pointer to raw data (00000C00 to 000011FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
40000040 flags
Initialized Data
Read Only
RAW DATA #2
SECTION HEADER #3
.reloc name
C virtual size
6000 virtual address (00406000 to 0040600B)
200 size of raw data
1200 file pointer to raw data (00001200 to 000013FF)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
42000040 flags
Initialized Data
Discardable
Read Only
RAW DATA #3
BASE RELOCATIONS #3
2000 RVA, C SizeOfBlock
940 HIGHLOW 00402000
0 ABS
Summary
2000 .reloc
2000 .rsrc
2000 .text