-   Linux - General (
-   -   linux stack usage (

xafwodahs 01-07-2007 09:02 PM

linux stack usage
Hi, I'm fairly new to linux but I am a long time programmer.

I'm trying to understand how linux handles stacks. (Assume 2.6.x).

First, I'd like to know how to determine how large a particular process/thread's stack is. I found that I can 'cat /proc/####/status | grep -i vmstk" and get 'a number', but after several tests, I don't trust that this is the stack size. (I've found it hard to get good documentation on what VmStk is...)

Another method I tried to figure out approx how large a default stack is, was this program (reproduced from memory, so might have a few mistakes...) :

void use_more_stack(void)
static int total_kb = 0;

char buffer[1024];
memset(buffer, 1, sizeof(buffer));

printf("total: %d KB\n", ++total_kb);


int main(void)
char buffer[1024];
return 0;

and then just waited for the seg fault or whatever bad thing happened.

Well, this program finally segfaulted after 12 MB (yes megabytes!).

So, can a linux thread have a 12MB stack? Does the stack grow dynamically with use? What's the deal?

Any info would be appreciated.

Thanks. 01-08-2007 05:58 AM

It looks as though the kernel you're running allows a 12MB stack. Linux does expand the stack space as needed.

For more information:

Hope this helps.

erichlund 05-11-2009 02:51 PM

For anyone reviewing this thread, note also, that the stack size in this case was for a single threaded program. Stack size allocated to additional threads would likely be much smaller, and would be bounded by its stack address and the previous stacks address. The first threads stack is, if I understand correctly, only bounded when it starts to run into stuff in the application space that sits underneath it.

All times are GMT -5. The time now is 02:41 PM.