Erik
Erik

Reputation: 3

C++ leaking memory with boost::ptr_vector

When opening up the task manager to see the memory usage, it will begin with .3MB memory used. After adding 100,000 objects to the ptr_vector, there is 2.3MB of memory used. After using .clear() on the vector, the memory goes to .8-.9MB. What is the cause of this increased memory usage?

Here's the code:

#include <iostream>
#include <boost/ptr_container/ptr_vector.hpp>

class T {
public:
    static int nObjects;
    T() {
        nObjects++;
    }
    ~T(){
        nObjects--;
    }
};

int T:: nObjects = 0;

int main() {
    boost::ptr_vector<T> vec;
    char pause;
    std::cout << "Press any key to continue: ";
    std::cin >> pause;

    for(int i=0; i<100000; i++) {
        vec.push_back(new T);
    }

    std::cout << T::nObjects << " objects created." << std::endl;

    std::cout << "Press any key to continue: ";
    std::cin >> pause;

    vec.clear();
    std::cout << T::nObjects << std::endl;

    std::cout << "Press any key to exit: ";
    std::cin >> pause;
    return 0;
}

Thanks for the help.

Upvotes: 0

Views: 659

Answers (3)

zindorsky
zindorsky

Reputation: 1592

There is another big chunk of memory that it seems you're forgetting about: the space allocated in vec to hold the pointers.

This statement:

vec.clear();

deleted all the Ts that you created with new, but it did not release the memory that vec itself allocated to hold all those pointers. If you call vec.capacity() you'll see that vec still has enough space to hold at least 100000 T*.

In order to free up that memory, use the swap idiom:

boost::ptr_vector<T>().swap( vec );

This creates a temporary ptr_vector which then takes vec's internal storage in the swap() call. Then when the temporary is destroyed at the ;, all the storage is released.

Upvotes: 1

Mr. Beer
Mr. Beer

Reputation: 255

From the looks of it, you are using boost::ptr_vector as it should.

I suggest you use Windows Perfmon to properly identify your application memory usage. See this blog for instructions how to use Perfmon in Windows 7.

Upvotes: 2

Yohan Danvin
Yohan Danvin

Reputation: 910

You cannot rely on the windows task manager for memory usage, at least not that way.

Probably the memory pages are kept, in case you need to allocate memory again.

Try to run what's in your main in a for loop (construction of vec + automatic destruction within the loop).
If it keeps increasing, then you may have a memory leak indeed.

But I doubt it will, unless you use boost::ptr_vector incorrectly.

Upvotes: 2

Related Questions