Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 620
X-Ray Avatar asked Apr 29 '10 23:04

X-Ray


People also ask

How do you measure stack size?

size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack.

How much stack memory is available?

It depends on your operating system. On Windows, the typical maximum size for a stack is 1MB, whereas it is 8MB on a typical modern Linux, although those values are adjustable in various ways.

What is the default stack size?

The default stack reservation size used by the linker is 1 MB. To specify a different default stack reservation size for all threads and fibers, use the STACKSIZE statement in the module definition (. def) file.


3 Answers

{$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;
like image 69
JGuzman Avatar answered Oct 02 '22 18:10

JGuzman


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.

like image 21
500 - Internal Server Error Avatar answered Oct 02 '22 18:10

500 - Internal Server Error


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.

like image 45
frogb Avatar answered Oct 02 '22 18:10

frogb