简体   繁体   English

QVector(或std :: vector)保留(或调整大小)不会释放内存

[英]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.

相关问题 如何使用 std::vector <std::tuple<A,B> &gt; 管理内存(调整大小,保留,...),但实际上将 As 保持在 Bs 之前,连续 - How to use std::vector<std::tuple<A,B>> to manage memory (resize, reserve,…), but actually keeping the As before the Bs, contiguously std :: vector调整大小与保留期间的保留时间 - std::vector resize vs reserve during fread std::vector 保留和调整 NUMA 位置的大小 - std::vector reserve & resize NUMA locality 当您想减小向量的大小时,为什么std :: vector :: resize()不分配内存? - Why doesn't std::vector::resize() deallocate the memory when you want to reduce the size of the vector? std::vector::resize() 与 std::vector::reserve() - std::vector::resize() vs. std::vector::reserve() 奇怪的内存表现为调整大小并保留关于向量的行为 - strange memory behave of resize and reserve about vector 在 std::vector::reserve 之后分配的 memory 的 memset - memset of allocated memory after std::vector::reserve 将 std::vector 复制到 qvector - copying a std::vector to a qvector 在Windows下无法测量由std :: vector &lt;&gt; :: reserve引起的内存分配 - Can't measure memory allocation caused by std::vector<>::reserve under Windows 调整大小并保留在向量中 - resize and reserve in vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM