即使在我从 qInt 队列中弹出所有元素后,以下代码也没有释放 3000 个元素消耗的内存。是什么原因 ?
std::queue<int> qInt; //Step01: Check the running memory
for (int i=0;i<3000;i++)
{
qInt.push(i);
}
//Step02: Check the running memory it should have been increased
while(!qInt.empty())
{
qInt.pop();
}
//Step03: Check the running memory expecting Step01 memory but it is still the same of Step02
最佳答案
默认情况下,std 容器在保留内存后不会释放内存。 std::queue 通常在提供 shrink_to_fit 的 std::dequeue 类型上实现。 .如果您不使用 C++ 11,请使用 swap idiom .
关于c++ - std::queue 内存消耗导致内存泄漏 - C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964251/