当打开任务管理器查看内存使用情况时,它将以使用的.3MB内存开始。在将10万个对象添加到ptr_vector后,使用了2.3MB的内存。在vector上使用.clear()之后,内存将变为.8-.9MB。内存使用量增加的原因是什么?使用boost :: ptr_vector导致C++内存泄漏
下面的代码:
#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;
}
感谢您的帮助。
非常感谢!这占增加的内存使用量的大部分。额外的一点可能是由于Yohan对内存页面的回答。 – Erik