How to Measure the Stack Size of a Process

Stack Size Estimation

Runtime-Evaluation

An online method is to paint the complete stack with a certain value, like 0xAAAA (or 0xAA, whatever your width is). Then you can check how large the stack has maximally grown in the past by checking how much of the painting is left untouched.

Have a look at this link for an explanation with illustration.

The advantage is that it's simple. A disadvantage is that you cannot be certain that your stack size won't eventually exceed the amount of used stack during your testing.

Static Evaluation

There are some static checks and I think there even exists a hacked gcc version that tries to do this. The only thing I can tell you is that static checking is very difficult to do in the general case.

Also have a look at this question.

How to determine maximum stack usage?

The most common way to determine the deepest stack usage is to initialize the stack memory with some known but unusual value, then periodically (or at the end of a big test run) see where that pattern stops.

This is exactly how the IAR IDE determines the amount of stack used.

Get the size of heap and stack per process in Linux

On Linux, you can read /proc/[PID]/maps and find [heap] and [stack] entries.

But for the GLIBC heap implementations usually used on Linux, the "heap" consists of both memory obtained via sbrk() that shows up in the /proc/[PID]/maps file as [heap] and memory obtained via mmap() - see this quesiton. So the "size" of the heap is going to be very hard to determine with certainty.

And the region labelled [stack] in the maps file is the stack for the main thread only. Multithreaded processes will have multiple stacks, one for each thread. And they will show up in the maps file as anonymous memory - maybe. The application can control the memory used for a thread's stack via the use of pthread_attr_setstack() and set it to any memory the application might use.

How to measure a functions stack usage in C?

Using warnings

This is GCC specific (tested with gcc 4.9):

Add this above the function:

#pragma GCC diagnostic error "-Wframe-larger-than="

Which reports errors such as:

error: the frame size of 272 bytes is larger than 1 bytes [-Werror=frame-larger-than=]

While a slightly odd way method, you can at least do this quickly while editing the file.

Using CFLAGS

You can add -fstack-usage to your CFLAGS, which then writes out text files along side the object files.
See: https://gcc.gnu.org/onlinedocs/gnat_ugn/Static-Stack-Usage-Analysis.html
While this works very well, its may be a little inconvenient depending on your buildsystem/configuration - to build a single file with a different CFLAG, though this can of course be automated.
– (thanks to @nos's comment)


Note,

It seems most/all of the compiler natural methods rely on guessing - which isn't 100% sure to remain accurate after optimizations, so this at least gives a definitive answer using a free compiler.

How to determine Stack size of a Program in linux?

If you simply want the current stack size, you could declare a variable at the top of main(), take its address, and compare it to the address of a variable declared at wherever you define "current" to be. The difference should be the approximate size that the stack has grown.

If you want to know how much memory is reserved for the stack, you can check /proc/[pid]/maps, which has a region marked as [stack]. For example, my atd process has:

7fff72a41000-7fff72a56000 rw-p 00000000 00:00 0                          [stack]
0175b000-0177c000 rw-p 00000000 00:00 0 [heap]

which gives you an idea.

A neat trick that a friend shared with me when I wanted to know the maximum size of stack that my program used was as follows. I'll present it here in case someone finds it useful :)

1) In a function called near the beginning of main(), use alloca() or a very long array to scribble 0xDEADBEEF or some other such unlikely constant over as much of the stack as you expect could be used. This memory will be "freed" when the small function returns.

2) At the end of main, again use alloca() to grab a region of memory and "search" down through it for whatever magic constant you used to scribble (you might try to find the first block of 64 of them or something to skip over regions of memory that may have been allocated but simply never used), and where that pointer lands indicates your maximum stack usage.

Not perfect, but it was useful for what I was doing!

Is there a limit of stack size of a process in linux

The stack is normally limited by a resource limit. You can see what the default settings are on your installation using ulimit -a:

stack size              (kbytes, -s) 8192

(this shows that mine is 8MB, which is huge).

If you remove or increase that limit, you still won't be able to use all the RAM in the machine for the stack - the stack grows downward from a point near the top of your process's address space, and at some point it will run into your code, heap or loaded libraries.



Related Topics



Leave a reply



Submit