Reputation: 17948
So I've noticed that the default stack size for threads on linux is 8MB (if I'm wrong, PLEASE correct me), and, incidentally, 1MB on Windows. This is quite bad for my application, as on a 4-core processor that means 64 MB is space is used JUST for threads! The worst part is, I'm never using more than 100kb of stack per thread (I abuse the heap a LOT ;)).
My solution right now is to limit the stack size of threads. However, I have no idea how to do this portably. Just for context, I'm using Boost.Thread for my threading needs. I'm okay with a little bit of #ifdef hell, but I'd like to know how to do it easily first.
Basically, I want something like this (where windows_* is linked on windows builds, and posix_* is linked under linux builds)
// windows_stack_limiter.c
int limit_stack_size()
{
// Windows impl.
return 0;
}
// posix_stack_limiter.c
int limit_stack_size()
{
// Linux impl.
return 0;
}
// stack_limiter.cpp
int limit_stack_size();
static volatile int placeholder = limit_stack_size();
How do I flesh out those functions? Or, alternatively, am I just doing this entirely wrong? Remember I have no control over the actual thread creation (no new params to CreateThread on Windows), as I'm using Boost.Thread.
Upvotes: 4
Views: 7775
Reputation: 231
I've run into similar problems on 32-bit systems (especially MIPS) running large application programs with hundreds of threads. Large default stacks don't tie up physical memory, but virtual memory can be a scarce resource as well. There are a couple of ways to resolve the problem:
Upvotes: 1
Reputation: 1700
First, you don't need to change this unless you are getting SEGVs from hitting this limit. (see man setrlimit
for detailed info)
Second, you change this in all of the linux distributions I'm aware of by editing /etc/security/limits.conf
(to change the default) or by running ulimit -s <stack size in kilobytes>
to change the value until you exit the shell.
Upvotes: 0
Reputation: 19044
You do not need to do this. The machine's physical memory is employed only where it is needed by a demand page fault system. Even if the thread stacks are significantly larger than the amount you are using the extra size is in virtual address space and does not tie up physical RAM.
Had physical RAM been tied up at that rate, a typical machine would run out of memory with only a few dozen processes running. You can see from a ps -Al
that quite a few more than that execute concurrently.
Upvotes: 7