Memory Manage in QML
is there any good solution to manage memory in QML?
the problem i got is that:
i have a ListView in QML, and it displays some images in it. i will change the image data dynamic, adding or removing.
i found that, i can't manage memory which was used by images. when i remove images from model, the memory will not be released.
unfortunely, the image data i managed is big. so, the memory cost is high.
so, u guys have any good idea?
Using your own image provider would be the way to go, I think.
Look for "image provider" in the documentation for an example.
i tried iamge provider as your suggestion.
since the provider will return a instance for each request, i still can not manage the memory.
for example, method "requestImage" will return a copy of QImage, the copy will be used by QML. i don't know how to release the copy of QImage when i wanna release it.
but a good news is that, i can use cache property of "Image" to prevent loading same images.
but even i use cache property, and i use same images, every time i clear my model and reload, the memory used by app increases.
Have you tried called "gc()" from your QML code? This should force the garbage collection and will clean up your images.
please what is gc() ? I didn't find any docs
i tried to use gc() in my code, but it didn't work.
it seems that the truth is as sierdzio said, it doesn't apply to QML. that's why it didn't work on releasing image memory.
besides image memory leak, it seems that list model in QML also causes memory leak.
the list model, i mean , it's my own list model, which is based on QAbstractListModel.
Strange, we used list models and images all over our application I don't see a memory leak. The usage will increase up to the size of the image cache (in our use case) but will then level off. I have left ours showing a new image every second for a day and the usage was stable.
Take a look at the "QtQuick2 performance documentation":http://doc-snapshot.qt-project.org/5.0/qtquick-performance.html for information about gc() etc. Also, some hooks to control the size of the default image cache, and purge it of currently unreferenced pixmaps, were added to QtQuick2 recently.