10

For example, on OSX, it's even less than 512k.

Is there any recommended size, having in mind, that the app does not use recursion and does not allocate a lot of stack variables?
I know the question is too broad and it highly depends on the usage, but still wanted to ask, as I was wondering if there's some hidden/internal/system reason behind this huge number.


I was wondering, as I intend to change the stack size to 512 KiB in my app - this still sounds like a huge number for this, but it's much smaller than 8MiB - and will lead to significantly decreased virtual memory of the process, as I have a lot of threads (I/O).

I also know this doesn't really hurt, well explained here: Default stack size for pthreads

  • Are you using a 32-bit CPU? X86_64 CPUs offer a virtual address space of up to 128 terabytes (in user space), which should be enough for lots of 8 MB stacks. – Johan Myréen Oct 5 '18 at 11:02
  • @JohanMyréen - no, x64 it is. It's not a big deal, I was just wondering, there's no real reason to do that (at the moment). – Kiril Kirov Oct 5 '18 at 11:09
  • In 2019 and 8 MiB is a lot of memory? I don't think so. Having a large default stack size makes it very easy to write programs with recursion. I was very surprised to know default stack size on Windows is only 1MiB! – oldherl Jun 13 at 9:46
15

As others have said, and as is mentioned in the link you provide in your question, having an 8MiB stack doesn’t hurt anything (apart from consuming address space — on a 64-bit system that won’t matter).

Linux has used 8MiB stacks for a very long time; the change was introduced in version 1.3.7 of the kernel, in July 1995. Back then it was presented as introducing a limit, previously there wasn’t one:

Limit the stack by to some sane default: root can always increase this limit if needed.. 8MB seems reasonable.

On Linux, the stack limit also affects the size of program arguments and the environment, which are limited to one quarter of the stack limit; the kernel enforces a minimum of 32 pages for the arguments and environment.

For threads, if the stack limit (RLIMIT_STACK) is unlimited, pthread_create applies its own limits to new threads’ stacks — and on most architectures, that’s less than 8MiB.

  • 1
    Wow, interesting. I thought that was recently introduced. I have about 200 threads (that's another long topic, so let's just ignore it for the moment) and top shows in scary VIRT results. Although, digging a bit deeper, the majority of this virtual address space is taken from the per thread (memory) arenas, not from the stack size, so lowering the stack size won't drastically reduce the virtual memory. I was just curious why 8MiB and why that much. – Kiril Kirov Oct 5 '18 at 13:22
  • The "8 MB" just means each thread's stack can grow to 8 MB if the thread decides to use it. But physical memory won't get allocated until the memory is actually used. If your 200 threads use 512 KB each, you'll use 100 MB physical ram, not 1.6 GB. – Guntram Blohm supports Monica Oct 5 '18 at 19:04
  • If you're not swapping, then the RES column in top gives you a much better answer to "what memory is this process actually using" than VIRT does. – kbolino Oct 5 '18 at 19:13
  • 1
    @Guntram the OP is well aware of that, see the link in the question. – Stephen Kitt Oct 5 '18 at 19:31
1

8MB is the virtual size of the stack. A page fault will happen when your application tries to use more stack than is currently physically allocated. The kernel's page fault handler will then allocated a physical page and then your application will continue.

See https://unix.stackexchange.com/a/280865/21212 for a complete explanation.

So reducing your stack size should have no effect in reducing physical memory usage of your application.

  • 1
    I have already linked this answer in my question. I also wrote, that I'm aware of this, but this doesn't actually answer the question. Thanks anyway – Kiril Kirov Oct 5 '18 at 13:16
  • I think you need to revisit the premises of the question, and this (non-)answer points to why. Virtual memory is not real memory. The stack size could be 800MB and it wouldn't affect actual memory usage, unless your application created more than 8MB worth of stack frames. – kbolino Oct 5 '18 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.