Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase stack size in Linux with setrlimit

Tags:

stack

gnu

reading information about how to increase stack size for a c++ application compiled with gnu, at compilation time, I understood that it can be done with setrlimit at the beginning of the program. Nevertheless I could not find any successful example on how to use it and in which part of the program apply it in order to get a 64M stack size for a c++ program, could anybody help me?

Thanlks

like image 717
asdf Avatar asked Feb 17 '10 07:02

asdf


People also ask

Can you increase the stack size?

You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).

What is stack size in Ulimit?

The stack size limit is the maximum size of the stack for a process, in units of 1024 bytes. The stack is a per-thread resource that has unlimited hard and soft limits.

What is the maximum stack size 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

Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:

  • call getrlimit to get current stack size
  • if current size < required stack size then
    • call setrlimit to increase stack size to required size

In C that might be coded something like this:

#include <sys/resource.h> #include <stdio.h>  int main (int argc, char **argv) {     const rlim_t kStackSize = 64L * 1024L * 1024L;   // min stack size = 64 Mb     struct rlimit rl;     int result;      result = getrlimit(RLIMIT_STACK, &rl);     if (result == 0)     {         if (rl.rlim_cur < kStackSize)         {             rl.rlim_cur = kStackSize;             result = setrlimit(RLIMIT_STACK, &rl);             if (result != 0)             {                 fprintf(stderr, "setrlimit returned result = %d\n", result);             }         }     }      // ...      return 0; } 
like image 179
Paul R Avatar answered Oct 12 '22 13:10

Paul R


See if the runtime execution maximum is limiting it:

[wally@zf conf]$  ulimit -all core file size          (blocks, -c) 0 data seg size           (kbytes, -d) unlimited scheduling priority             (-e) 0 file size               (blocks, -f) unlimited pending signals                 (-i) 16114 max locked memory       (kbytes, -l) 32 max memory size         (kbytes, -m) unlimited open files                      (-n) 1024 pipe size            (512 bytes, -p) 8 POSIX message queues     (bytes, -q) 819200 real-time priority              (-r) 0 stack size              (kbytes, -s) 10240 cpu time               (seconds, -t) unlimited max user processes              (-u) 16114 virtual memory          (kbytes, -v) unlimited file locks                      (-x) unlimited 

Note that the stack size, by default, is limited to 10 MiB. So to increase it to 64 MiB:

[wally@zf conf]$ ulimit -s 64M -bash: ulimit: 64M: invalid number [wally@zf conf]$ ulimit -s 65536 [wally@zf conf]$ ulimit -all core file size          (blocks, -c) 0 data seg size           (kbytes, -d) unlimited scheduling priority             (-e) 0 file size               (blocks, -f) unlimited pending signals                 (-i) 16114 max locked memory       (kbytes, -l) 32 max memory size         (kbytes, -m) unlimited open files                      (-n) 1024 pipe size            (512 bytes, -p) 8 POSIX message queues     (bytes, -q) 819200 real-time priority              (-r) 0 stack size              (kbytes, -s) 65536 cpu time               (seconds, -t) unlimited max user processes              (-u) 16114 virtual memory          (kbytes, -v) unlimited file locks                      (-x) unlimited 
like image 20
wallyk Avatar answered Oct 12 '22 14:10

wallyk