PDA

View Full Version : QML Memory Bloat from Component/Object Creation



mdomschot
3rd October 2016, 17:30
It appears that QML is bloating in memory from dynamic object create using Qt.createComponent() and createObject(). I've created a simplified test program that shows this. It is available here: https://github.com/mdomschot/qml_mem

Note: I'm using Qt 5.4.0 on Windows 7 and Ubuntu 14.04. It appears in both environments.

I have 6 panels. They are identical except for a title and are mostly just there to consume memory by filling in with fake data. I provide buttons to switch between any them *and* the ability to go to no panel, essentially a blank screen.

Aside from all code being available to build/run in the git repo linked, here are a few of the main snippets (from main.qml)...
1) This is where panels are placed into the GUI.


Item {
id: panelArea

property var item: undefined
property string source: ""
}

2) This is how I create the panels.


var panelSource = "Panel%1.qml".arg(panel)
var component = Qt.createComponent(Qt.resolvedUrl(panelSource), this)
panelArea.item = component.createObject(panelArea, { })
panelArea.source = panelSource
component.destroy()

3) This is how I destroy the panels.


panelArea.item.destroy()
panelArea.item = undefined
panelArea.source = ""


I also provide buttons to call gc(), QQmlEngine::trimComponentCache(), and QQmlEngine::clearComponentCache(). All three seem to have little to no effect.

Below is a chart of my data from a single run on Ubuntu. It shows the memory increase over time as I jump between panels. Each panel is a separate line (1-6) and the state where there is no panel is the '-' line.
12138

I've tried several variants of dynamically loading/unloading panels, including a Loader object, and all have the memory effect. Am I doing something wrong? Is there a method I can use to clean up memory? I fully expect memory to go up as I load all 6 panels. But after destroying all 6, I imagine there is some way to have QML relinquish memory...