如何在.net中找到当前线程的最大堆栈大小? [英] How to find current thread's max stack size in .net?

查看:69
本文介绍了如何在.net中找到当前线程的最大堆栈大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到当前线程的最大堆栈大小?

How can I find current thread's maximum stack size?

从MMC UI执行功能时,而不是从Powershell(命令行/控制台)执行功能时,出现堆栈溢出异常.因此,我有点猜测这与UI线程中分配给Powershell的默认堆栈大小(命令行/控制台)有关.

I am getting a stack overflow exception while executing a function from MMC UI but not from Powershell (command-line/console). So I am kind of guessing its something to do with the default stack size allocated in UI thread to that of in Powershell (command line/console).

那么如何找到当前线程的最大堆栈大小?

So how to find current thread's maximum stack size?

我知道理想情况下不需要知道这些或进行设置,但是看起来它与堆栈大小有关,因为它是通过控制台/Powershell(命令行应用程序)而不是通过UI工作的.

I know ideally one doesn't need to know these or set these, but looks like its related to stack size as it works from console/Powershell(command-line app) not from UI.

下面的线程有点相关,但是没有回答我的问题;它可能会提供一些指导:

The below thread is kind of related, but it doesn't answer my question; it probably gives some guidelines:

最大线程堆栈大小.NET?

要获取有关实际问题的更多详细信息:

To get more details about the actual problem:

StackOverFlowException:是编程错误(递归)最大默认堆栈大小是否不够?

推荐答案

在Windows 8中,有

From Windows 8, there is the GetCurrentThreadStackLimits() function. You can use it from C# via PInvoke like this:

[DllImport("kernel32.dll")]
static extern void GetCurrentThreadStackLimits(out uint lowLimit, out uint highLimit);

uint low;
uint high;

GetCurrentThreadStackLimits(out low, out high);
var size = (high - low) / 1024; // in KB

在我的计算机上,这在控制台应用程序中产生1MB的空间,在Web应用程序(IIS)中产生256KB的空间.

On my machine, this yields 1MB in a console application, 256KB in a web application (IIS).

这篇关于如何在.net中找到当前线程的最大堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆