简体   繁体   中英

Possible memory leak when using a vector of a map of strings C++

I have a pretty complex data object that uses has a map of strings

typedef std::map<std::string, unsigned int> Event;
typedef std::pair<double, Event> EventGroup;
std::vector<EventGroup> eventVector;

This is a program that's always running in the background listening to incoming messages. Every time a new EventGroup comes in, which can have any number of strings in the map, I add it to the vector.

// New data came in
eventVector.push_back(newEventGroup);

Every now and then I'll do an erase on this vector

//Flush some of the data because it's old
// it's been determined that the index to erase at is flushIndex
eventVector.erase(eventVector.begin(), eventVector.begin()+flushIndex);

Typically this tends to be the first 5% of the data.

What I've been noticing is that there seems to be a memory leak. The memory usage starts out around 50 MB... but ends up near 1 GB before it's too slow and crashes. I've heard that it's an expensive operation to do an erase, but could this be the cause of the memory leak? Am I missing some way of freeing up the memory used by the map?

Without knowing what your custom types do or look like (are THEY leaking memory?) it's hard to say. You should note however that erasing elements from a vector does not actually free any memory, it makes the area the vector has already allocated available for different elements added to THAT vector. The vector's reserved space remains the same in other words.

So, if you grow a vector to some million elements, erase 90% of them, and are expecting to get a bunch of memory back you'll be disappointed. The way you can free up memory reserved by a vector (which will NEVER give anything back until it's destroyed) is to do the swap idiom thing:

std::vector<EventGroup>(eventVector).swap(eventVector);

I don't recall the exact specifics of how the copy constructor works here. It should behave exactly the same as if you'd done this:

std::vector<EventGroup>(eventVector.begin(), eventVector.end()).swap(eventVector);

You still have no control over how much space this uses up, but if you've freed a lot of space up and it will remain freed for a long while...this should give some unknown amount of memory back to the system.

Keep in mind that this is an expensive operation (which is why std::vector doesn't just do it for you) so only do it when you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM