Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine Stack size of a Program in linux?

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?

like image 754
Sashi Avatar asked Nov 05 '09 07:11

Sashi


People also ask

How do you determine 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 do you calculate stack in a program?

For calculating (and therefore optimizing) the required stack memory size, the following methods may be used: • Static analysis (using call tree analysis) is performed at build time (by a linker for example). Dynamic analysis (using stack watermarking) is performed at run-time (in a debug session for example).

What is the stack size in Linux?

The stack size, referes to how much space is allocated in memory for the stack. If you increase the stack size, that allows the program to increase the number of routines that can be called. Each time a function is called, data can be added to the stack (stacked on top of the last routines data.)

What is stack size in programming?

Stacks are temporary memory address spaces used to hold arguments and automatic variables during invocation of a subprogram or function reference. In general, the default main stack size is 8 megabytes.


1 Answers

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!

like image 154
Steven Schlansker Avatar answered Oct 01 '22 13:10

Steven Schlansker