samedi 27 mai 2017

C++ Thread inside a for loop causing big memory leak

I have been trying out using threads in my code and I've found that there was a memory leak after I added threads. Usually, the code took up 400MB, but after I added threads it rose to 900MB and continued to increase.

I made a test code that to see where the memory leak was coming from and I found it to be the thread inside the for loop. After a short wait, this test code used over 100MB and kept growing. This code also slightly resembles the one I was working on.

#include <iostream>
#include <thread>

void foo(){}

int main()
{
    for(;;){
        std::thread Ding(foo);
        Ding.join();
    }
}

I am using Code::Blocks 16.01 and used task manager to see the memory usage.

Why does this code continuously increase its memory usage and how can I stop it?

Aucun commentaire:

Enregistrer un commentaire