mauriceofadown
mauriceofadown

Reputation: 23

memory leak std::vector management c++

I'm getting a memory leak when I create a 50'000 values vector of double, and I don't know why.

#include <stdafx.h>
#include <Windows.h>
#include <psapi.h>

#define MEMLOGINIT  double mem1, mem2;\
                    PROCESS_MEMORY_COUNTERS_EX pmc;\
                    GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));\
                    SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;\
                    mem1 = virtualMemUsedByMe/1024.0;\
                    std::cout << "1st measure \n Memory used : " << mem1 <<" Ko.\n\n";\

#define MEMLOG(stepName)    GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));\
                            virtualMemUsedByMe = pmc.PrivateUsage; \
                            mem2 = virtualMemUsedByMe/1024.0; \
                            std::cout << stepName << "\n Memory used : " << mem2 << " Ko.\n Difference with previous measure : " << mem2 - mem1 <<" Ko.\n\n";\
                            mem1 = mem2;



int _tmain(int argc, _TCHAR* argv[])
{   
    MEMLOGINIT;
    {
        vector<double> spTestmemo(50000 ,100.0);
        MEMLOG("measure before destruction");
    }   
    MEMLOG("measure after destruction");
};

output with 50k values Clearly here the 400 ko allocated by the vector aren't released.

However, the destructor works with a vector of 500'000 values.

int _tmain(int argc, _TCHAR* argv[])
{   
    MEMLOGINIT;
    {
        //vector<double> spTestmemo(50000 ,100.0);
        vector<double> spTestmemo(500000 ,100.0); //instead of the line above
        MEMLOG("measure before destruction");
    }   
    MEMLOG("measure after destruction");
};

output with 500k values Here, a vector ten times bigger than the previous one is almost completely destroyed (small bias of 4 ko).

Thanks for your help.

Upvotes: 2

Views: 321

Answers (1)

Sami Sallinen
Sami Sallinen

Reputation: 3496

As NathanOlivier and PaulMcKenzie pointed out in their comments, this is not a memory leak.

The c++ std library may not be releasing all the memory to the OS when you free it, but the memory is still being accounted for.

So don't worry too much about what you see as the OS reported virtual memory usage of your program as long as it is not abnormally high or continuously increasing while your program runs.

--- begin visual studio specific:

Since you seem to be building your code with Visual Studio, its debug runtime library has a facilities for doing what you are doing with your MEMLOGINIT and MEMLOG macros, see https://msdn.microsoft.com/en-us/library/974tc9t1.aspx#BKMK_Check_for_heap_integrity_and_memory_leaks

Basically you can use _CrtMemCheckpoint to get the status of what has been allocated, and _CrtMemDifference and _CrtMemDumpStastistics to compare and log the difference between 2 checkpoints.

The debug version of the runtime library also automatically dumps leaked memory to the debugger console of your program when the program exits. If you define new as DEBUG_NEW it will even log the source file and line number where each leaked allocation was made. That is often very valuable when hunting down memory leaks.

Upvotes: 2

Related Questions