Increasing the stack size in shell is resulting in an increase in virtual memory size for multithreaded applications.

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux

Issue

  • Increasing the stack size in shell is resulting in an increase in virtual memory size for multithreaded applications.

With default stack size,

# ulimit -s
10240

# ps -o pid,ppid,pcpu,rss,size,vsize,cputime,lstart -Lp 31861
  PID  PPID %CPU   RSS    SZ    VSZ     TIME                  STARTED
31861 31779  0.0   584 2148664 2154332 00:00:00 Wed Oct  3 11:44:32 2012
31861 31779  0.0   584 2148664 2154332 00:00:00 Wed Oct  3 11:44:32 2012
31861 31779  0.0   584 2148664 2154332 00:00:00 Wed Oct  3 11:44:32 2012
31861 31779  0.0   584 2148664 2154332 00:00:00 Wed Oct  3 11:44:32 2012
31861 31779  0.0   584 2148664 2154332 00:00:00 Wed Oct  3 11:44:32 2012
31861 31779  0.0   584 2148664 2154332 00:00:00 Wed Oct  3 11:44:32 2012

Increasing the stack size using ulimit,

# ulimit -s 1000240
# ./a.out 5
[..]

# ps -o pid,ppid,pcpu,rss,size,vsize,cputime,lstart -Lp 31913
  PID  PPID %CPU   RSS    SZ    VSZ     TIME                  STARTED
31913 31779  0.0   584 7098644 7104312 00:00:00 Wed Oct  3 11:49:57 2012
31913 31779  0.0   584 7098644 7104312 00:00:00 Wed Oct  3 11:49:57 2012
31913 31779  0.0   584 7098644 7104312 00:00:00 Wed Oct  3 11:49:57 2012
31913 31779  0.0   584 7098644 7104312 00:00:00 Wed Oct  3 11:49:57 2012
31913 31779  0.0   584 7098644 7104312 00:00:00 Wed Oct  3 11:49:57 2012
31913 31779  0.0   584 7098644 7104312 00:00:00 Wed Oct  3 11:49:57 2012

Resolution

This is an expected behaviour.

From 'man pthread_create',

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.  Using pthread_attr_setstacksize(3), the stack size attribute can be explicitly set in the attr argument used to create a thread, in order to obtain a stack size other than the default.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments