Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the stack size of a process?

Tags:

linux

How do I find the stack size of a process ? /proc/5848/status gives me VmStk but this doesnt change

No matter how much ever while loop and recursion I do in my test program this value hardly changes.

when I looked at /proc/pid/status all of the process has 136k and have no idea where that value comes from.

Thanks,

like image 221
resultsway Avatar asked Dec 06 '12 17:12

resultsway


People also ask

How do you calculate stack size of process?

There really is no such thing as the "stack size of a process" on Linux. Processes have a starting stack, but as you see, they rarely allocate much from the standard stack. Instead, processes just allocate generic memory from the operating system and use it as a stack.

How do I know my max 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.

What is stack size?

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.

What is the size of the stack in Linux?

On Linux/x86-32, the default stack size for a new thread is 2 megabytes. Under the NPTL threading implementation, if the RLIMIT_STACK soft resource limit at the time the program started has any value other than "unlimited", then it determines the default stack size of new threads.


2 Answers

There really is no such thing as the "stack size of a process" on Linux. Processes have a starting stack, but as you see, they rarely allocate much from the standard stack. Instead, processes just allocate generic memory from the operating system and use it as a stack. So there's no way for the OS to know -- that detail is only visible from inside the process.

A typical, modern OS may have a stack size limit of 8MB imposed by the operating system. Yet processes routinely allocate much larger objects on their stack. That's because the application is using a stack that is purely application-managed and not a stack as far as the OS is concerned.

This is always true for multi-threaded processes. For single-threaded processes, it's possible they are actually just using very, very little stack.

like image 185
David Schwartz Avatar answered Oct 19 '22 20:10

David Schwartz


Maybe you just want to get the address map of some process. For process 1234, read sequentially the /proc/1234/maps pseudo-file. For your own process, read /proc/self/maps

Try

 cat /proc/self/maps

to get a feeling of it (the above command displays the address map of the cat process executing it).

Read proc(5) man page for details.

You might also be interested by process limits, e.g. getrlimit(2) and related syscalls.

I am not sure that stack size has some precise sense, notably for multi-threaded processes.

Maybe you are interested in mmap(2)-ed segments with MAP_GROWSDOWN.

like image 40
Basile Starynkevitch Avatar answered Oct 19 '22 20:10

Basile Starynkevitch