簡體   English   中英

std :: queue內存消耗會導致內存泄漏 - C ++?

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

即使在我彈出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

通過defalut std容器一旦保留它就不會釋放內存。 std :: queue通常在std :: dequeue類型上實現,它提供了shrink_to_fit 如果你不使用c ++ 11,請使用交換習語

如果你釋放/釋放/刪除堆內存。 這並不意味着內存消耗會立即降低。 內存管理庫有自己的可用內存緩存,它們會在達到閾值后釋放。

首先,3000整數使用的內存非常低,如果使用任務管理器檢查內存,則無法看到內存使用量的顯着變化。 另外,如其他答案中所解釋的,STL容器不會立即解除分配。 有一個很好的論壇討論STL對象和對象指針的內存分配和釋放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM