Pkp
Pkp

Reputation: 969

stack size from proc/pid/maps in C

I saw the stack size from /proc/pid/maps for a C program in linux-64 bit. I could see the following line pertaining to stack size.

7fffc2e14000-7fffc2e35000 rw-p 00000000 00:00 0 [stack]

I am not understanding the above values. If i take the difference I get difference 135168 in decimal. But this is not close to 8MB. Am i interpreting it wrong?

But if i print the rlimit:

int main (void)
{
  struct rlimit limit;

  getrlimit (RLIMIT_STACK, &limit);
  printf ("\nStack Limit = %ld and %ld max\n", limit.rlim_cur, limit.rlim_max);
}

Output: Stack Limit = 8388608 and -1 max

I get Stack Limit = 8388608 which comes up to 8MB.

Upvotes: 5

Views: 3022

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215257

Linux does not commit the whole rlimit size for the stack when a process starts. It merely reserves this much virtual address space (so that other mmaps, etc. can't block the stack from growing) and lets the stack dynamically grow up to the limit. However, there is a default committed stack size too, which seems to be 128k or 132k depending on the system, and I have no idea how to tweak/tune that.

Upvotes: 2

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137418

rlim_cur is a "soft limit" on what the stack for that process can grow to. It is not an indicator of the current stack usage. From the man page:

Each resource has an associated soft and hard limit, as defined by the rlimit structure:

struct rlimit {
    rlim_t rlim_cur;  /* Soft limit */
    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
};

The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit.

/proc/[pid]/maps on the other hand, shows you what is currently mapped in for that process. I see 0x21000 which is most likely (33) 4k pages. This is probably a better indicator of your current stack usage for that process. However, it probably includes things other than the stack, such as the environment variables.

Upvotes: 4

Pavan Manjunath
Pavan Manjunath

Reputation: 28535

RLIMIT_STACK specifies the maximum limit upto which your stack can grow. Right now your stack is at 132 KB. If you had used more of auto variables, the stack size you saw at /proc/pid/maps would have been more.

Upvotes: 0

Related Questions