I am programming something in C that creates a lot of Pthreads in Linux on a 256Mb system. I usually have +200Mb free.
When I run the program with a low amount of threads it works, but once I make it create around 100 threads it gives errors because the system runs out of memory. I did several tests and each threads use almost 2Mb. The stack size of the threads is set to 16Kb.
The code I use to create each thread:
pthread_attr_t attr;
pthread_attr_init(&attr);
size_t stacksize;
stacksize = (double) 16*1024;
int res = pthread_attr_setstacksize (&attr, stacksize);
int res2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (res != 0 || res2 != 0) {
logs << "pthread_attr_XX: error "+int2string(res);
exit(-1);
}
pthread_t id;
pthread_create(&id, &attr, &Class::thread_callback, &some_var);
Is it normal or am I missing something? Thanks.
select()
. – Rifling__thread
specifier)? This can come from libraries too. Do your threads use a lot memory from malloc? Is it really no error returned in res? Your value is exactly the biggest prohibited value according to line 74 of this file. The check is "/* Catch invalid sizes. */
\nif (stacksize < PTHREAD_STACK_MIN)
\nreturn EINVAL;
" – Tepidstrace -f
orltrace -S -f
of your program and post results online? Any memory allocation will be visible in such traces. – Tepid