简体   繁体   中英

In C# How to estimate the maximum stack size when constructing Thread?

Create a Thread in C#, the constructor has a parameter maxStackSize, I don't know how big its value should be, how should I estimate the value of maxStackSize? Constructor like this: public Thread (System.Threading.ThreadStart start, int maxStackSize);

The default value of the stack is 1MB, and there is very little reason to make it bigger. A bigger stack might be needed if you must have a very deep recursion, for example. However, then 4MB (the maximum size possible) might not able to help either and you better look at a different algorithm.

The idea to define the stack is mostly to define size smaller than 1MB. Since the stack must be allocated in physical memory, when the thread is running, if you know your code is small and doesn't have recursion or deep calls, you can specify a smaller stack size.

In general, just go with the default stack size.

EDIT: NET CLR (starting 4.0) doesn't commit the full 1MB in memory. In commits only 100K, expecting these will be enough; however, it does reserve 1MB of virtual address space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM