Is it possible in c # and in general to allocate memory in the stack for a separate thread - more or less than 1 MB? I was just told that it is impossible to allocate any more (well, okay), but no less. They say that the processor has five modes of operation (I did not go into details) - and for some of these modes on some kind of architecture (the word is empty for me - I still don’t understand this), you can still allocate more / less memory than 1 mb. Is it so? in the video course I’m going through, the author went inside the Thread class to demonstrate that this class has 4 constructors, where he paid attention to this one:

public Thread(ParameterizedThreadStart start, int maxStackSize); 

in which the second parameter indicates the size of the stack allocated for the stream. But at the same time I said that we can’t specify no more than no less than a megabyte - because ".... this is the structure (not literally struct - but in the portable) addressing memory in a protected processor mode" - as I understand it, that this "protected mode" is the standard mode of the processor ?!

But at the same time he said that we can’t specify no more than no less than a megabyte - because ".... such is the structure (not literally struct - but in the portable) addressing memory in a protected processor mode" - this is where the delivered In this topic question.

  • Terminological moment: it is not memory in the stack that is allocated for a thread, this is for a thread when it starts, (its own) stack of a certain size is allocated. - VladD
  • in Sharpe (that is, in Windows) - I do not know, but in Linux it is easy to change with the help of ulimit in any direction. I suspect that in Windows too. Here they write - msdn.microsoft.com/ru-ru/library/5cykbwz4(v=vs.110).aspx - that can be changed, but not all and not always. - KoVadim
  • If you are given an exhaustive answer, mark it as correct (tick the selected answer). - andreycha

1 answer 1

Do not read Soviet newspapers before lunch.

  1. The default stack size for a 32-bit code is 1 MB, for a 64-bit code it is 4 MB.
  2. With the help of the constructor you mentioned, the stack size can be set to any, but with some restrictions:
    • size less than the minimum (256 KB) will not be taken into account and the stack will be allocated the minimum amount of memory
    • there must be a continuous block of free memory of the specified size
    • stack size is rounded to the size of the page (usually 64 KB)
    • the code should work in full trust if you want to set the size larger than the default
  3. If you don’t go into the modes of operation of the processor and the processor architecture is an empty word for you, you will not need the above-mentioned information at all ( however, like most of those who understand ).

Illustration :

 class Program { private struct MEMORY_BASIC_INFORMATION { public uint BaseAddress; public uint AllocationBase; public uint AllocationProtect; public uint RegionSize; public uint State; public uint Protect; public uint Type; } private const uint STACK_RESERVED_SPACE = 4096 * 16; private const int MEGABYTE = 1024 * 1024; [DllImport("kernel32.dll")] private static extern int VirtualQuery( IntPtr lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, int dwLength); static void Main(string[] args) { Console.WriteLine("Main thread"); Console.WriteLine(EstimatedRemainingStackSizeInMegabytes()); Console.WriteLine("New thread"); var thread = new Thread( () => Console.WriteLine(EstimatedRemainingStackSizeInMegabytes()), 16 * MEGABYTE); thread.Start(); thread.Join(); Console.ReadLine(); } private unsafe static float EstimatedRemainingStackSizeInMegabytes() { MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION(); IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096); VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION)); return (float)(currentAddr.ToInt64() - stackInfo.AllocationBase - STACK_RESERVED_SPACE) / MEGABYTE; } } 

Result:

Main thread

0.9277534

New thread

15.93015

  • I also advise you to add information on the disableStackOverflowProbing parameter and possible crutches when using this constructor to complete the picture. - Alexis