How to Determine Stack Size of a Program in Linux

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!

How do I find the maximum stack size?

You can query the maximum process and stack sizes using getrlimit. Stack frames don't have a fixed size; it depends on how much local data (i.e., local variables) each frame needs.

To do this on the command-line, you can use ulimit.

If you want to read these values for a running process, I don't know of any tool that does this, but it's easy enough to query the /proc filesystem:

cat /proc/<pid>/limits

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.

Determine a thread's stack size and location

Here's a different method of doing this, involving reading /proc/self/maps. Unlike some of the other methods, it doesn't require special instrumentation at the start of your program and gives you an exact position for the end of the stack.

If you try cat /proc/self/maps, you get something like this:

00400000-0040c000 r-xp 00000000 08:01 6039736              /usr/bin/cat
0060b000-0060c000 r--p 0000b000 08:01 6039736 /usr/bin/cat
0060c000-0060d000 rw-p 0000c000 08:01 6039736 /usr/bin/cat
00908000-00929000 rw-p 00000000 00:00 0 [heap]
7fcdb1c68000-7fcdb1e01000 r-xp 00000000 08:01 6032628 /usr/lib/libc-2.21.so
7fcdb1e01000-7fcdb2000000 ---p 00199000 08:01 6032628 /usr/lib/libc-2.21.so
7fcdb2000000-7fcdb2004000 r--p 00198000 08:01 6032628 /usr/lib/libc-2.21.so
7fcdb2004000-7fcdb2006000 rw-p 0019c000 08:01 6032628 /usr/lib/libc-2.21.so
7fcdb2006000-7fcdb200a000 rw-p 00000000 00:00 0
7fcdb200a000-7fcdb202c000 r-xp 00000000 08:01 6032717 /usr/lib/ld-2.21.so
7fcdb21f5000-7fcdb21f8000 rw-p 00000000 00:00 0
7fcdb2209000-7fcdb222b000 rw-p 00000000 00:00 0
7fcdb222b000-7fcdb222c000 r--p 00021000 08:01 6032717 /usr/lib/ld-2.21.so
7fcdb222c000-7fcdb222d000 rw-p 00022000 08:01 6032717 /usr/lib/ld-2.21.so
7fcdb222d000-7fcdb222e000 rw-p 00000000 00:00 0
7ffe78c41000-7ffe78c62000 rw-p 00000000 00:00 0 [stack]
7ffe78dba000-7ffe78dbc000 r--p 00000000 00:00 0 [vvar]
7ffe78dbc000-7ffe78dbe000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

As you can see, there is a [stack] entry. This is probably what you're looking for.

An example program to parse this line out:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[]) {
FILE *file = fopen("/proc/self/maps", "r");
char line[1024];
void *result = NULL;
while (!feof(file)) {
if (fgets(line, sizeof(line) / sizeof(char), file) == NULL) {
break;
}
unsigned long start, end, offset;
unsigned int devma, devmi, ino;
char perms[6];
char path[128];
if (sscanf(line, "%lx-%lx %5s %lx %d:%d %d %127s", &start, &end, &perms, &offset, &devma, &devmi, &ino, &path) != 8) {
continue; // could not parse. fail gracefully and try again on the next line.
}
if (strcmp(path, "[stack]") == 0) { // use [stack:TID] for a thread besides the main thread
printf("Stack found from %lx to %lx\n", start, end);
break;
}
}
fclose(file);
return 0;
}

This will print something like:

Stack found from 7fff91834000 to 7fff91855000

This is probably fairly close to what you're looking for.

check used stack size using core file

If you think your stack size limit has been reached due to infinite recursion, an easy way to determine this is to run bt or info stack and see if there are many more frames than you expect. Here's an example where I recursively called a function with around 1024 bytes of local data in each stack frame:

(gdb) info stack
#0 recurse () at loop.c:5
#1 0x0000000000400565 in recurse () at loop.c:5
...
#7939 0x0000000000400565 in recurse () at loop.c:5
#7940 0x000000000040058f in main (argc=1, argv=0x7ffe63afef48) at loop.c:10

You can also exceed the stack size limit with just a few frames if you have a large amount of local data in the frames.

To check the approximate size of the stack, on Linux/x86 you can check the difference between _environ, which contains a high address that's fairly close to the base of the stack, and $sp, the stack pointer in the current frame (top of stack).

(gdb) print (char *)_environ - (char *)$sp
$5 = 8384904

This looks pretty close to the stack size limit of 8MB.

(gdb) shell ulimit -s
8192

You can also look at the difference between the $sp in the frame at the top of the stack and the $sp in the frame at the base of the stack.

(gdb) frame 0
#0 recurse () at loop.c:5
(gdb) set $topsp=$sp
(gdb) frame 7940
#7940 0x000000000040058f in main (argc=1, argv=0x7ffe63afef48) at loop.c:10
(gdb) print (char *)$sp - (char *)$topsp
$6 = 8384640

Checking available stack size in C

Raymond Chen (The Old New Thing) has a good answer to this sort of question:

If you have to ask, you're probably doing something wrong.

Here's some Win32 details on stack allocation: MSDN.

If you think you might be limited by stack space, you will almost certainly be limited by available virtual memory, in which case, you will need to find a different solution.

What exactly are you trying to do?



Related Topics



Leave a reply



Submit