【发布时间】:2014-03-24 17:28:08
【问题描述】:
即使在我从 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
【问题讨论】:
-
你在用任务管理器检查内存消耗吗?
-
@Borgleader 当然是的
-
您永远不会知道,您可能会将 3000 多个项目推入该队列。如果您希望最好的机会释放堆内存以准备即将发生的假设,请将其与本地空自动交换。
-
@WhozCraig 你提出了这样的方法。 std::queue
为空; std::swap(qInt, 空);如果我想清空整个队列,那很好。假设每当我使用 pop 时,如何减少为推送 3000 个元素分配的内存? -
@Carthi 查看重新运行的答案和链接。它(他们)比我在评论中解释得更好。
标签: c++ visual-c++ memory stl queue