views:

227

answers:

2

How does one determine the current stack size of a program in linux?

it is said that the stack size of each program will be 8 MB in linux but when you use cat /proc//mmap it shows a different size.

Also, how does one determine stack size of associated threads? Since it is said that threads have their own private stack?

+1  A: 

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!

Steven Schlansker
Won't the "neat trick" fail if VM pages in the "freed" stack area are reclaimed by the kernel and reallocated?
Dipstick
I don't believe that the kernel will ever try to reclaim stack area. In fact, I think that the stack itself is just a (extremely widespread) convention between the compiler / assembler / standard libraries, so I'm not sure that the kernel could "prove" sufficiently that it's safe to reclaim.
Steven Schlansker
I don't see how it could not be "safe" to reclaim any unused pages above the top of stack (assuming of course that the machine supports demand paging - which most linux capable machines do).Obviously there would be a lot of unnecessary page faults to handle if all the pages above the current top of stack were reclaimed by the kernel but it would probably only be necessary to keep one or two spares available if running short on memory. Swapping "freed" stack pages in and out of disk would make no sense at all.
Dipstick
The stack "convention" is so widespread that even the OS uses it for exception handling, so in theory it could be freed (paging it out would make no sense). However even if it is, what are the chances that your app requires an **exact** multiple of the page size? In almost all cases there will be some remnant of dead beef at the top of the last page, so this trick should almost always work.
zildjohn01
+1  A: 

As suggested Steven, there is a difference between the stack size reserved for your thread and the stack that is currently used by your thread.

If you want to know how much memory is reserved for one thread you can use pthread attribute.

pthread_attr_t attr;
size_t stacksize;

pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr, &stacksize); 

printf("%u\n", stacksize);

This will print the default stack size reserved when creating one thread. For me it is 8 Mb.

You can change this by using pthread_attr_setstacksize(), and passing the attr structure as 2 arguments to the pthread_create function.

Edit : Maybe you should also be aware of lazy allocation issues. Your 8 Mb of virtual space won't use 8Mb of physical memory space unless you read or write everywhere in this memory space.

Ben