什么是安全的最大堆栈大小或如何衡量堆栈的使用? [英] What is a safe Maximum Stack Size or How to measure use of stack?

查看:33
本文介绍了什么是安全的最大堆栈大小或如何衡量堆栈的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个工作线程的应用程序,每个内核一个.在现代 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;

另一个我没有的想法.

这篇关于什么是安全的最大堆栈大小或如何衡量堆栈的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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