Memory use with repeated creation/destruction of ListView's
-
Hi, everyone.
Qt 4.7.3, Windows
Qt 4.7.2, Linux fedoraI have a problem with a program we are currently developing here. We repeatedly create and destroy ListView's and it seems that memory is leaking.
Our code is in C++ (with QDeclarativeComponent::create), but one of my co-workers wrote a QML-only version that exhibits the problem in QMLViewer. I attached the two QML files to the present message. Load the memory_leak.qml file in QMLViewer and press Return repeatedly (or hold it down) to create ListView's from TemplateListView.qml.
If you check the memory used by the QMLViewer process, you will see it increase in time. You can add pictures in the model/view to make it more obvious.
What are we doing wrong?
2 Files below:
main.qml
@
import QtQuick 1.0Item{
id: rootfunction createList(){ var qtComponent = Qt.createComponent("TemplateListView.qml"); var qtObject = qtComponent.createObject(container); if (qtObject == null){ console.log("Error in createList() : qtObject is null !"); return; } container.isEmpty = false; } width: 500 height: 600 focus: true Keys.onReturnPressed: if (container.isEmpty) createList() Text{ anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.top anchors.topMargin: 40 width: 500 height: 40 text: "press Return to create a listview and the listview will auto destructs after 100 ms." } Item{ id: container property bool isEmpty: true anchors.top: parent.top anchors.topMargin: 100 width: 500 height: 500 } ListModel{ id: myModel ListElement{ label: "label1" } ListElement{ label: "label2" } ListElement{ label: "label3" } ListElement{ label: "label4" } ListElement{ label: "label5" } ListElement{ label: "label6" } ListElement{ label: "label7" } ListElement{ label: "label8" } ListElement{ label: "label9" } ListElement{ label: "label10" } }
}
@TemplateListView.qml
@
import QtQuick 1.0ListView{
id: templateanchors.fill: parent opacity: 1 focus: false model: myModel delegate: Component{ Rectangle{ width: template.width height: 100 color: "grey" Text{ text: label } } } highlight: Rectangle { color: "black" } Component.onCompleted: opacity = 0; Behavior on opacity { NumberAnimation { duration: 100 onRunningChanged: if (!running) {template.parent.isEmpty = true;template.destroy();} } }
}
@--
Serge -
I've had some similar concerns about Loaders. In my code I'm constantly changing screens and using Loaders to create/destroy new/current screens. When I look at my memory manager (top and task manager) all I see is a climbing number. It never seems to go back down.
I think behind the scenes model-based QML elements also load visual elements dynamically.
I use visual leak detector on the Windows side and it reports no leaks. Is the cause a lazy garbage collection algorithm?
-
Hi,
A couple notes:
- Because of the way QML works, it is normal to see memory usage increase up to a point. This is due to things like image caches and a garbage collected JS environment. There is a gc() function you can call from QML to manually trigger a garbage collection, if you'd like to see if it helps.
- From memory there were a number of memory leaks fixed post 4.7.3, such as http://qt.gitorious.org/qt/qt/commit/3750d24175904916fe5ab8b1a76db956b1904e72 and http://qt.gitorious.org/qt/qt/commit/340c22999d9f9678c9035c1dc372423e970b8ca7. I'm not sure if these make any difference in your particular cases, but if you are able you could also try the 4.8 beta and see if things are improved there.
Regards,
Michael -
Still facing this same problem in qt 6.2 . when i push and remove page in stack view ,program memory kept rising and never go down even after manually call to gc();
The page contain toolbar,image ,rectangle and buttons and doesn't contain any custom qml type .