简体   繁体   English

使用字符串C ++映射的向量时可能发生内存泄漏

[英]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. 每当一个新的EventGroup进入时,它可以在地图中包含任意数量的字符串,我将它添加到向量中。

// 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. 通常,这往往是数据的前5%。

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. 内存使用量开始大约50 MB ......但是在它太慢并且崩溃之前最终接近1 GB。 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. 但是,您应该注意,从向量中删除元素实际上并不释放任何内存,它使向量已经分配的区域可用于添加到THAT向量的不同元素。 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. 因此,如果你将一个向量增加到几百万个元素,删除它们中的90%,并期望得到一堆内存,你会感到失望。 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. 请记住,这是一个昂贵的操作(这就是std :: vector不仅仅为你做的原因)所以只有在需要时才这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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