It seems that the recommended way to set stack size for a C program or Ruby program (which uses the C stack), is by using ulimit in the Bash shell. But
$ ulimit -s
8192
$ ulimit -s 16384
-bash: ulimit: stack size: cannot modify limit: Operation not permitted
and sudo doesn’t help either. Is there a way to set it to 16MB, 32MB, or 64MB? I thought there should be a way to set it per program invocation instead of setting a system wide parameter as well?
Right now 8192 probably means 8MB which is quite small, if that is compared to how much a process can be using, sometimes as much as 2GB of RAM.
(updated note: ulimit -a can show its current values).
(update 2: it actually seems like ulimit -s <value> is per shell, and that if you set it the first time, it usually works. The problem is when you set it the second time, then it may return an error)
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.
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:
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.