我可以问一个运行线程,以了解它使用了多少堆栈?

发布于 2025-01-24 15:43:09 字数 108 浏览 0 评论 0 原文

因此,假设我已经在Windows OS上创建了一个线程,我知道默认的堆栈大小远远超过了所需的大小。然后,我可以运行应用程序并询问线程实际使用的堆栈量,以便我知道我应该设置其堆栈大小而不是默认的堆栈大小?

So let's say I have created a thread, on Windows OS, for which I know that the default stack size is much more than what is needed. Can I then run the application and ask the thread about the amount of stack it actually has used so that I know how much I should set its stack size instead of the default stack size?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-31 15:43:09

Windows通常不会 commit 整个堆栈,它只 reveres it。 (好吧,除非您要求它,例如,通过指定 createThread 的非零堆栈大小参数,而无需传递 stack_size_param_is_is_a_a_reservation flag)。

您可以使用它来弄清线程在运行时曾经需要多少堆叠量,包括任何CRT,Winapi或第三方库的电话。

为此,只需读取 stackbase stacklimit teb中的值 - 请参见这个问题如何做。两个值的差异应该是已订入的堆栈内存量,即 - 线程实际使用使用的堆栈内存量。

另外,如果手动过程足够:只需在WINDBG中启动该应用程序,请在线程退出之前设置断点,然后用!TEB 命令将TEB丢弃。您还可以用!地址将整个内存映射倾倒,并查找使用usage stack 的订员区域。

Windows usually doesn't commit the entire stack, it only reserves it. (Well, unless you ask it to, e.g. by specifying a non-zero stack size argument for CreateThread without also passing the STACK_SIZE_PARAM_IS_A_RESERVATION flag).

You can use this to figure out how much stack your thread has ever needed while running, including any CRT, WinAPI or third-party library calls.

To do that, simply read the StackBase and StackLimit values from the TEB - see answers to this question for how to do that. The difference of the two values should be the amount of stack memory that has been committed, i.e. - the amount of stack memory that the thread has actually used.

Alternatively, if a manual process is sufficient: Simply start the application in WinDBG, set a breakpoint before the thread exits and then dump the TEB with the !teb command. You can also dump the entire memory map with !address and look for committed areas with usage Stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文