how can i see how much of the stack space is currently used in my delphi app?
Asked Answered
K

3

10

how can i see how much of the stack space is currently used in my delphi app? i had a very strange error that sounds like stack trouble. i'd like to add it to my app's log to get some idea how much stack space is in use/remaining. using the debugger is probably not so great because the routine can be called many times.

thank you!

Keratinize answered 29/4, 2010 at 23:46 Comment(0)
J
2
{$IFDEF MSWINDOWS}
function currentStackUsage: NativeUInt;
//NB: Win32 uses FS, Win64 uses GS as base for Thread Information Block.
asm
  {$IFDEF WIN32}
  mov eax, fs:[4]  // TIB: base of the stack
  sub eax, esp     // compute difference in EAX (=Result)
  {$ENDIF}
  {$IFDEF WIN64}
  mov rax, gs:[8]  // TIB: base of the stack
  sub rax, rsp     // compute difference in RAX (=Result)
  {$ENDIF}
{$ENDIF}
end;
Jedlicka answered 9/12, 2019 at 15:36 Comment(0)
R
12

This should give you your current stack usage:

function CurrentStackUsage: DWord;
asm
  mov eax, fs:[4]
  sub eax, esp
end;

I don't remember off the top of my head a simple way to get the max stack size at run-time, but you have the default value in your linker options.

Redmond answered 30/4, 2010 at 0:11 Comment(2)
thank you for your reply! i am surprised how small the value returned by this is. it would indicate the stack is at best about 1% used. is this number in bytes?Keratinize
thank you,why this dont changed? and what is meaning of 3500 of returned by my app?!Franconian
S
3

VMmap from SysInternals can give you a graphical view of each type of memory used by your application, including stack. It does not give you the exact usage like the function in Per Larsen's answer, but may help you to visualize memory usage at different stages of your application.

Scorcher answered 30/4, 2010 at 8:57 Comment(1)
interesting app; i can learn something new from that. thanks!Keratinize
J
2
{$IFDEF MSWINDOWS}
function currentStackUsage: NativeUInt;
//NB: Win32 uses FS, Win64 uses GS as base for Thread Information Block.
asm
  {$IFDEF WIN32}
  mov eax, fs:[4]  // TIB: base of the stack
  sub eax, esp     // compute difference in EAX (=Result)
  {$ENDIF}
  {$IFDEF WIN64}
  mov rax, gs:[8]  // TIB: base of the stack
  sub rax, rsp     // compute difference in RAX (=Result)
  {$ENDIF}
{$ENDIF}
end;
Jedlicka answered 9/12, 2019 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.