mercredi 24 juillet 2019

c++ thread can not reuse deallocated memory that allocated by main

I have a problem when create new object by main thread and then delete it, after that I create it again using the same function by Thread and found that my memory(monitor from htop) grow up. Am I wrong on this code or this is its behavior?

void createAndDeleteObj()
{
    std::vector<SomeObj *> objVector;

    //   create object
    for( size_t i = 0; i < 1000000; ++i )
    {
        objVector.emplace_back( new SomeObj() );
    }

    //   delete object
    for( size_t i = 0; i < 1000000; ++i )
    {
        delete objVector[i];
    }
}

void main()
{

     createAndDeleteObj();

     //  memory not increase
     createAndDeleteObj();

     // memory increase equal to the first time used
     Thread thread1( createAndDeleteObj );

     thread1.join();

     // call this function by thread again, memory not increase 
     Thread thread2( createAndDeleteObj );

     thread2.join();

     //  memory not increase
     createAndDeleteObj();
}

Since the concept of Thread share the heap space, i dont know why it always allocate new memory for Thread, why it does not use memory allocated by main even if it is freed.

Aucun commentaire:

Enregistrer un commentaire