std::queue memory consumption leads to memory leak - C++ ?

◇◆丶佛笑我妖孽 提交于 2019-12-05 18:15:26

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.

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 .

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!