什么是安全的最大堆栈大小或如何衡量堆栈的使用?堆栈、大小、安全、最大

2023-09-07 03:05:06 作者:怀念童年时的裸奔

我有一个带有多个工作线程的应用程序,每个内核一个.在现代 8 核机器上,我有 8 个这样的线程.我的应用加载了很多插件,它们也有自己的工作线程.因为该应用程序使用大量内存(照片,例如 200 MB),所以我有内存碎片问题.问题是每个线程都分配了 {$MAXSTACKSIZE ...} 的地址空间.它不是使用物理内存,而是使用地址空间.我将 MAXSTACKSIZE 从 1MB 减少到 128KB,它似乎可以工作,但如果我接近极限,我现在就不行了.有没有可能衡量实际使用了多少堆栈?

I have an app with a number of worker threads, one for each core. On a modern 8 core machine, I have 8 of these threads. My app loads a lot of plugins, which also have their own worker threads. Because the app uses huge blocks of memory (photos, eg. 200 MB) I have memory fragmentation problem. The problem is that every thread allocates the {$MAXSTACKSIZE ...} of address space. It's not using the physical memory but the adress space. I reduced the MAXSTACKSIZE from 1MB to 128KB and it seems to work, but I don't now if I'm near to the limit. Is there any possibility to measure how much stack is really used?

推荐答案

使用它来计算当前线程堆栈提交的内存量:

Use this to compute the amount of memory committed for the current thread's stack:

function CommittedStackSize: Cardinal;
asm
  mov eax,[fs:$4] // base of the stack, from the Thread Environment Block (TEB)
  mov edx,[fs:$8] // address of lowest committed stack page
                  // this gets lower as you use more stack
  sub eax,edx
end;

另一个我没有的想法.