Reputation: 265
The following codes didn't release the memory consumed for 3000 elements even after i pops out all the elements from the qInt queue. What is the reason ?
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
Upvotes: 5
Views: 4331
Reputation: 1507
First of all the memory used by 3000 integers is very low and can't see a significant change in memory usage if you are checking memory using Task Manager . Also , as explained in other answers the STL containers do not deallocate immediately. There is a nice forum discussing memory allocation and deallocation by STL objects and object pointers.
Upvotes: 0
Reputation: 179
if you release/free/delete a heap memory . it doesn't mean that the memory consumption will immediately come down . the memory management libraries have there own caches of free memory which they would release after reaching a threshold .
Upvotes: 2
Reputation: 25505
By defalut std containers do not deallocate memory once they have reserved it. The std::queue is generally implemented on type of std::dequeue which offers shrink_to_fit. If you are not using c++ 11 use the swap idiom.
Upvotes: 9