Kiril Kirov
Kiril Kirov

Reputation: 38193

pthread_create allocates a lot of memory in Linux?

Here's a simple example

#include <iostream>
#include <thread>
#include <vector>
#include <chrono>

void* run(void*)
{
    while (true)
        std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main()
{
    std::vector<pthread_t> workers(192);

    for (unsigned i = 0; i < workers.size(); ++i)
        pthread_create(&workers[i], nullptr, &run, nullptr);

    pthread_join(workers.back(), nullptr);
}

top shows 1'889'356 KiB VIRT! I know this isn't resident memory, but still, this is huge amount of memory for a single thread creation.

Is it really so memory-expensive (8MiB in this case) to create a thread? Is this configurable?

Or, maybe and most probably, I have some misunderstanding what virtual memory is?


Details:

I double quadruple-checked the memory usage, using:

As this size matches the max size of a stack (also confirmed by /proc/<pid>/limits), I tried to make the max size of the stack smaller. Tried with 1 MiB, but this didn't change anything.

Please, put aside the creation and usage of 192 threads, it has a reason behind it.

Sorry for the mixed C and C++ - initially tried with std::thread and the results are the same.

Upvotes: 3

Views: 1322

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

pthread_attr_setstacksize() function is available to set stack size. This function have to be used with an thread attribute object. The thread attribute object has to be passed as 2nd argument of pthread_create().

#include <iostream>
#include <thread>
#include <vector>
#include <chrono>

void* run(void*)
{
    while (true)
        std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main()
{
    std::vector<pthread_t> workers(192);
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setstacksize(&attr, 16384);

    for (unsigned i = 0; i < workers.size(); ++i)
        pthread_create(&workers[i], &attr, &run, nullptr);

    pthread_join(workers.back(), nullptr);
}

Upvotes: 1

Related Questions