How to change the stack size using ulimit or per process on Mac OS X for a C or Ruby program? How to change the stack size using ulimit or per process on Mac OS X for a C or Ruby program? c c

How to change the stack size using ulimit or per process on Mac OS X for a C or Ruby program?


Apparently there is a hard limit on the stack size for mac os x, taken from http://lists.apple.com/archives/scitech/2004/Oct/msg00124.html granted this is quite old, and Im not sure if its still true anymore, but to set it simply call ulimit -s hard, its 65532. or about 65 megs.

I did some tests on snow leopard, 10.6.8, and it does seem to be true.

$ ulimit -a...stack size              (kbytes, -s) 8192...$ ulimit -s 65533-bash: ulimit: stack size: cannot modify limit: Operation not permitted$ ulimit -s 65532$

I also found this http://linuxtoosx.blogspot.com/2010/10/stack-overflow-increasing-stack-limit.html though I haven't test it, so can't really say much about it.

When applications consume gigs of memory thats usually taken from the heap, the stack is usually reserve for local automatic variables that exist for a relatively small amount of time equivalent to the lifespan of the function call, the heap is where most of the persistent data lives.

here is a quick tutorial:

#include <stdlib.h>#define NUMBER_OF_BYTES 10000000 // about 10 megsvoid test(){   char stack_data[NUMBER_OF_BYTES];          // allocating on the stack.   char *heap_data = malloc(NUMBER_OF_BYTES); // pointer (heap_data) lives on the stack, the actual data lives on the heap.}int main(){       test();     // at this point stack_data[NUMBER_OF_BYTES] and *heap_data have being removed, but malloc(NUMBER_OF_BYTES) persists.    // depending on the calling convention either main or test are responssible for resetting the stack.    // on most compilers including gcc, the caller (main) is responssible.    return 0;}$ ulimit -a...stack size              (kbytes, -s) 8192...$ gcc m.c$ ./a.outSegmentation fault$ ulimit -s hard$ ./a.out$

ulimit is only temporary you would have to update it every time, or update your corresponding bash script to set it automatically.

Once ulimit is set it can only be lowered never raised.


To my mind the accepted answer is not totally right and leads to miss comprehension, more specifically the last statement is not true.

Once ulimit is set it can only be lowered never raised.

There are indeed soft (displayable with ulimit -s or ulimit -Ss) and hard (displayable with ulimit -Hs) limits.But while setting the limit through ulimit -s will affect soft and hard values.

Once hard limit is set it can only be lowered never raise, but soft limit can be lowered or raised provided that the value stays lower than the hard limit.

This will work:

# base values$ ulimit -s100$ ulimit -Hs100$ ulimit -Ss100# lower soft limit only$ ulimit -Ss 50$ ulimit -s50$ ulimit -Hs100$ ulimit -Ss50# raise soft limit only$ ulimit -Ss 100$ ulimit -s100$ ulimit -Hs100$ ulimit -Ss100# lower soft and hard limit$ ulimit -s 50$ ulimit -s50$ ulimit -Hs50$ ulimit -Ss50# then impossible to raise soft limit due to hard limit$ ulimit -s 100-bash: ulimit: stack size: cannot modify limit: Operation not permitted$ ulimit -Ss 100-bash: ulimit: stack size: cannot modify limit: Invalid argument


The system default stack size varies from different version of kernel to kernel. My 10.7 is 16384, such that ulimit -s 16384 is accepted by my Mac. You can try sysctl kern.stack_size and it shows the read-only stack size. mine is 16384.
You can see this technical article, http://developer.apple.com/library/mac/#qa/qa1419/_index.html, to see how to change the default stack size for C program. For Ruby, because it's a scripting language, you have to enlarge its stack size during linking Ruby interpreter.Excepting for having very deep function calls or recursion, or having very large array and objects being allocated in the stack, your program should not have huge stack space. Instead, using heap or dynamically allocation can use up to 2GB of RAM as you wish.