Changes

Jump to: navigation, search

Stack size

1,587 bytes added, 14:59, 8 April 2010
created (beta)
Applications using POSIX threads (a.k.a. NPTL threads, as implemented by glibc) reserves some amount of virtual memory for the stack per each thread. Unfortunately, glibc uses the maximum stack size (as configured by <code>ulimit -s</code> a.k.a. <code>setrlimit(RLIMIT_STACK, ...</code>) as the amount of memory to be reserved. In most of the new distributions this parameter is set to a very high value of 10240 (1K blocks), i.e. 10 megabytes. Therefore, glibc reserves 10MB of anonymous virtual memory per each thread, which can easily lead to <code>[[privvmpages]]</code> shortage if it's not set high enough.

== Getting the current stack size limit ==

You can see the current stack size limit (RLIMIT_STACK) by running <code>ulimit -s</code> command. Result is in kilobytes.

== Increasing privvmpages ==

The obvious solution is to increase privvmpages value by the amount enough for the given number of threads and the current value of RLIMIT_STACK. For example, if you plan to run 10 threads and stack size limit is set to 10240, you need to increase privvmpages for 10240*10/4, i.e. 25600 (4 is a page size, in kilobytes).

== Decreasing the maximum stack size ==

Most applications do not need a stack as big as 10 megabytes, so you can set it to a smaller value before starting your threaded app. One obvious place for doing that is application startup script, like /etc/init.d/mysqld or /etc/init.d/httpd. What you need to do is to add a line like

ulimit -s 1024 # Stack size limit is 1M

to the beginning of such a script.

[[Category: Beancounters]]
[[Category: HOWTO]]

Navigation menu