[英]QVector(or std::vector) reserve(or resize) down doesn't free memory
Say I have a QVector
and I call QVector::reserve for 420000 elements. 假设我有一个
QVector
并且我为420000个元素调用QVector :: reserve。 Right after that I call QVector::reserve
again for 42 elements. 之后,我再次为42个元素调用
QVector::reserve
。 What I noticed is that after doing this, the process still has memory allocated for those 420000 elements. 我注意到的是,这样做之后,进程仍然为这420000个元素分配了内存。 Why doesn't memory get freed when I shrink the
QVector
size to 42? 当我将
QVector
大小缩小到42时,为什么没有释放内存?
Take the following code: 采取以下代码:
#include <QCoreApplication>
#include <QVector>
#include <QDebug>
typedef QVector<QPointF> PointVec;
typedef QList<PointVec*> PointVecList;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Processing...";
PointVecList list;
for (int i = 0; i < 4200; i++) {
PointVec *v = new PointVec();
v->reserve(420000);
v->resize(420000);
v->reserve(42);
v->resize(42);
list.append(v);
}
qDebug() << "End of processing...";
return a.exec();
}
This code crashes throwing std::bad_aloc
exception( Unhandled exception at 0x7729C41F in QtConsoleApplication1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0034F5E8.
) on a 32 bit system(as the system doesn't support much RAM for one process). 此代码导致在32位系统上抛出
std::bad_aloc
异常( Unhandled exception at 0x7729C41F in QtConsoleApplication1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0034F5E8.
)(因为该系统不支持一个的大量RAM处理)。
However if I comment lines v->reserve(420000);
但是,如果我在
v->reserve(420000);
注释行v->reserve(420000);
and v->resize(420000);
和
v->resize(420000);
everything works fine. 一切正常。 This is how I noticed that if I call
v->reserve(42);
这就是我注意到的,如果我调用
v->reserve(42);
and v->resize(42);
和
v->resize(42);
(after reserving/resizing the vector to 420000 elements beforehand) exceeding memory doesn't get freed. (事先将向量保留/调整为420000个元素之后)不会释放超出内存的空间。
What I also noticed is that std::vector
behaves exactly the same. 我还注意到,
std::vector
行为完全相同。
Is it possible to force Qt
to also free the memory? 是否可以强制
Qt
释放内存? If not, is it possible to free it myself, manually, somehow? 如果没有,有可能自己手动释放它吗?
Unless there is actual memory pressure it makes sense for the underlying library to not do the work of actually releasing the allocated memory back to the OS but instead keep it around and use it to satisfy new allocations (without incurring a context switch to get it from the kernel) that you are likely to make soon anyway. 除非存在实际的内存压力,否则底层库不执行将分配的内存实际释放回操作系统的工作,而是保留它并使用它来满足新分配的工作(无需进行上下文切换即可从中获取它)内核)。
Same goes for the OS. 操作系统也是如此。 Even if the application actually releases the memory there's no point in doing the work of actually reclaiming it until it is actually needed elsewhere (with luck it never will be until the process exits and it can just do a single bulk reclaim of everything).
即使应用程序实际释放了内存,也没有必要进行实际回收的工作,直到在其他地方实际需要它为止(幸运的是,直到进程退出,它才可以对所有内容进行一次批量回收)。
So, the reason is efficiency. 因此,原因是效率。 There's a cost involved in actually freeing the memory and there's no reason to pay the cost until you really have to - which, in many cases will be never.
实际释放内存涉及成本,在您真正需要之前没有理由支付这笔费用-在许多情况下,这永远都是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.