當打開任務管理器查看內存使用情況時,它將以使用的.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