当动态创建的对象有父对象时,QML 不会释放大量内存 [英] QML does not free a lot of memory, when dynamically created object has a parent

查看:125
本文介绍了当动态创建的对象有父对象时,QML 不会释放大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Component.createObject() 方法动态创建一个对象.之后,我使用 destroy() 方法删除对象.
如果我在没有父参数的情况下调用方法 Component.createObject(null)(使用 null),则 destroy() 方法会释放内存.如果我使用一些 parent 调用该方法,那么 destroy() 方法不会释放大量内存.创建大量对象时,这会导致超出 Windows 操作系统中每个进程允许的内存量并导致应用崩溃.
重现问题的代码:
main.qml

I use Component.createObject() method to dynamically create an object. After that I delete the object using the destroy() method.
If I call the method Component.createObject(null) without parent argument (with null), then destroy() method frees the memory. If I call the method with some parent then destroy() method does not free a lot of memory. When creating a large number of objects, this results in exceeding the permissible amount of memory per process in Windows OS and leads to app crash.
Code to reproduce the problem:
main.qml

import QtQuick 2.8

Item {
    visible: true
    width: 640
    height: 480

    Scene {
        id: scene
        anchors.fill: parent
    }
}

Scene.qml

import QtQuick 2.8

Item {
    id: scene

    Component.onCompleted: {
        var tileComponent = Qt.createComponent("Tile.qml");

        for (var i = 0; i < 200000; ++i) {
            var tile = tileComponent.createObject(scene);
//          var tile = tileComponent.createObject(null); // this works well, but I need a parent
            tile.destroy();
        }
    }
}

Tile.qml

import QtQuick 2.8

Rectangle {
    width: 100
    height: 100

    color: "orange"
}

destroy() 之后,有父级的变体使用 40 MB 内存,没有父级 - 12 MB.如果我继续创建和销毁此类对象,则使用的内存会继续增长.
如何通过父对象动态创建和销毁大量对象,避免内存问题?
我已经在 Windows 7 x64 上测试了 Qt 5.9.1(MinGW 5.3.0 编译器)和 Qt 5.8.0(MSVC 2015).

The variant with parent uses 40 MB of memory after destroy(), without parent - 12 MB. If I continue to create and destroy such objects, the memory used continues to grow.
How can I dynamically create and destroy a lot of objects with parent avoiding the problem with memory?
I have tested Qt 5.9.1 (MinGW 5.3.0 compiler) and Qt 5.8.0 (MSVC 2015) on Windows 7 x64.

推荐答案

这种行为对于 Qt 来说是正常的.你不需要做任何事情.如果您继续测试,您会在某个时候看到内存使用量停止增长.

This behavior is normal for Qt. You do not need to do anything about it. If you would continue testing, you would at some point see that memory usage stopped growing.

我建议不要使用 gc() 因为它真的没有任何帮助.它强制 Qt 做一些它会在需要时自动做的事情.

I would advise against using gc() because it really does not help anything. It forces Qt to do something that it will do automatically when it needs to.

垃圾收集器可以通过在 JavaScript 中调用 gc() 来手动调用.这将导致执行一个全面的收集周期,可能需要几百到一千多毫秒才能完成,因此应尽可能避免.

The garbage collector may be invoked manually by calling gc() within JavaScript. This will cause a comprehensive collection cycle to be performed, which may take from between a few hundred to more than a thousand milliseconds to complete, and so should be avoided if at all possible.

来源

这篇关于当动态创建的对象有父对象时,QML 不会释放大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆