The .NET stack vs Windows stack(.NET 堆栈与 Windows 堆栈)
问题描述
Windows Internal book 5th edition在页面中有如下评论360.<上一页>初始线程的堆栈大小取自图像——没有办法指定另一个尺寸.
据我了解,对于 Windows 操作系统,每个线程被赋予 4K 或 16K(取决于系统)堆栈,并且大小是固定的.
那么 .NET 中的堆栈呢?
- 堆栈有多大?
- 栈的大小是固定的还是可变的?
- 是否像 Windows 一样为每个线程分配堆栈?
是的,启动线程的大小由 .EXE 文件头中的值决定.必然如此,创建线程的是操作系统,在程序中的任何代码都可以运行之前.它调用程序的入口点,CorExeMain().
您使用的托管编译器将该值写入 EXE 文件头.当前的 .NET 编译器在以 x86 或 Any CPU 为目标时选择 1 MB,在以 x64 为目标时选择 4 MB.然而,这不是固定的,您可以使用 Editbin.exe 实用程序/STACK 命令行选项修改该值.您可以使用此构建后事件来获取 2MB 堆栈:
设置路径=%path%;$(DevEnvDir);$(DevEnvDir)....vcineditbin.exe/STACK:2097152 "$(TargetPath)"
您自己创建的线程的堆栈大小由您控制,线程类构造函数 具有允许您指定大小的重载.如果将值裁剪为 256 KB,则不能使其太小.这是必要的,即时编译器也使用堆栈.
The Windows Internal book 5th edition has the following comment in page 360.
The stack size for the initial thread is taken from the image—there’s no way to specify another size.
I understand that for Windows OS, each thread is given 4K or 16K (depending on system) stack, and the size is fixed.
Then how about the stack in .NET?
- How big is the stack?
- The size of the stack is fixed or variable?
- Is the stack allocated for each thread just like the case of Windows?
Yes, the size for the startup thread is determined by a value in the .EXE file header. Necessarily so, it is the OS that creates the thread, before any code in the program can run. It calls the entrypoint of the program, CorExeMain().
The managed compiler you use writes that value into the EXE file header. Current .NET compilers select 1 MB when you target x86 or Any CPU, 4 MB when you target x64. This is however not fixed, you can modify the value with the Editbin.exe utility, /STACK command line option. You could use this post-build event to get a 2MB stack:
set path=%path%;$(DevEnvDir);$(DevEnvDir)....vcin
editbin.exe /STACK:2097152 "$(TargetPath)"
The stack size for threads that you create yourself are under your control, the Thread class constructor has overloads that lets you specify the size. You cannot make it too small, if clips the value to 256 KB. That's necessary, the just-in-time compiler also uses the stack.
这篇关于.NET 堆栈与 Windows 堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NET 堆栈与 Windows 堆栈
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01