Why on modern Linux, the default stack size is so huge - 8MB (even 10 on some distributions)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












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










share|improve this question





















  • 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
    3 hours ago










  • @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
    3 hours ago














up vote
4
down vote

favorite
1












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










share|improve this question





















  • 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
    3 hours ago










  • @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
    3 hours ago












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





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










share|improve this question













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







linux osx virtual-memory multithreading stack






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









Kiril Kirov

1384




1384











  • 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
    3 hours ago










  • @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
    3 hours ago
















  • 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
    3 hours ago










  • @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
    3 hours ago















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
3 hours ago




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
3 hours ago












@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
3 hours ago




@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
3 hours ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote













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.




Note that 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.






share|improve this answer




















  • 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
    1 hour ago


















up vote
-1
down vote













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.






share|improve this answer




















  • 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
    1 hour ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473416%2fwhy-on-modern-linux-the-default-stack-size-is-so-huge-8mb-even-10-on-some-di%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













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.




Note that 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.






share|improve this answer




















  • 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
    1 hour ago















up vote
3
down vote













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.




Note that 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.






share|improve this answer




















  • 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
    1 hour ago













up vote
3
down vote










up vote
3
down vote









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.




Note that 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.






share|improve this answer












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.




Note that 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Stephen Kitt

149k23328395




149k23328395











  • 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
    1 hour ago

















  • 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
    1 hour ago
















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
1 hour ago





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
1 hour ago













up vote
-1
down vote













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.






share|improve this answer




















  • 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
    1 hour ago














up vote
-1
down vote













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.






share|improve this answer




















  • 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
    1 hour ago












up vote
-1
down vote










up vote
-1
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 3 hours ago









Colin 't Hart

1158




1158











  • 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
    1 hour ago
















  • 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
    1 hour ago















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
1 hour ago




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
1 hour ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473416%2fwhy-on-modern-linux-the-default-stack-size-is-so-huge-8mb-even-10-on-some-di%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

One-line joke