简体   繁体   English

STL向量占用太多内存

[英]STL vector taking up too much memory

Im using a STL vector in my SDL program. 我在我的SDL程序中使用了STL向量。 and it looks like this: vector< Bullet * > vec; 它看起来像这样:vector <Bullet *> vec; this makes a vector that can contain pointers to Bullet objects. 这使得向量可以包含指向Bullet对象的指针。 when i run my program i only add one item at a time using: vec.push_back( new_bullet ); 当我运行程序时,一次只能使用以下一项添加一项:vec.push_back(new_bullet); (new_bullet is a pointer to a "new" Bullet object. then in a following function i erase an object using: vec.erase(...); vec.size() shows that the items are being pushed and popped properly. I'm running Ubuntu 9.10 and System Monitor shows my programs memory usage slowly increasing. Is it my program or something I'm missing about STL vector? (new_bullet是指向“新” Bullet对象的指针。然后在以下函数中,我使用vec.erase(...); vec.size()擦除对象,这表明项目已被正确推送和弹出。正在运行Ubuntu 9.10,系统监视器显示我的程序内存使用率缓慢增加。是我的程序还是STL vector缺少的内容?

听起来像是你不delete荷兰国际集团的“子弹头”的对象,你从载体中删除。

erase() only removes an element from a vector, it does not delete the memory pointed to if that element is a pointer. erase()仅从向量中删除一个元素,如果该元素是指针,它不会删除指向的内存。 This makes sense... imagine you had a vector<const char *> of string literals and did v.erase(i) ; 这很有道理...假设您有一个字符串文字的vector<const char *>并做了v.erase(i) you can't delete a const char * ! 您不能删除const char *

However, erase() will call the destructor for the element removed. 但是, erase() 将为删除的元素调用析构函数。 So if you used a sort of "pointer object" rather than a pointer, it could release the memory when destroyed. 因此,如果您使用某种“指针对象”而不是指针,则销毁该指针可能会释放内存。 If you're interested, you should check out boost::shared_ptr which implements reference counted garbage collection. 如果您有兴趣,应该检查一下boost::shared_ptr ,它实现了引用计数垃圾回收。

If you're only adding to the end of the array and you're worried about memory fragmentation, you might want to use std::list instead. 如果仅添加到数组的末尾,并且担心内存碎片,则可能要使用std::list代替。 The only problem is, list accesses are O(n) time if they're not at the beginning. 唯一的问题是,如果列表访问不在开头,则访问时间为O(n)。

Are you calling free()/delete on your bullets when you're done with them after pulling them out of the vector? 将子弹从向量中拉出后,使用完子弹后,您是否要在它们上调用free()/ delete?

While I'm guessing the original poster is not freeing his "popped" pointers (pop doesn't do that for you), std::vector has another "feature" that may cloud it issue. 当我猜想原始发布者没有释放他的“弹出”指针(pop不能为您做到这一点)时,std :: vector还有另一个“功能”,可能会使它模糊。 std::vector overallocate space such that every push_back doesn't entail a reallocation and copy of the existing contents. std :: vector总体分配空间,以使每个push_back都不需要重新分配和复制现有内容。 In other words, the space used for vector is generally more than what's returned by .size(). 换句话说,用于向量的空间通常大于.size()返回的空间。

Take a look at the shrink-to-fit idiom if you wish to make a std::vector take up no more space than it needs to hold its elements. 如果您希望使std :: vector占用的空间不超过容纳其元素所需的空间,请看一下“ 缩小适应”的习惯用法。 Not useful if you are constantly manipulating the vector, but once you have everything loaded, it can save some space. 如果您一直在操纵矢量,则没有用,但是一旦加载了所有内容,它可以节省一些空间。

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

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