From 23e0e26ce652cf360310ebb1e2de8283502eba2e Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 18 Apr 2016 15:14:26 +0200 Subject: Occasionally trim the type cache As loaded components are kept in a cache, they are never removed by the garbage collector. So, if you periodically create new components, they leak. This change adds a floating threshold for the number of components. When that threshold is surpassed trimCache() is called and unneeded components are removed. Task-number: QTBUG-42055 Change-Id: I30e3e4ee287f6d34376713668009c67614a50e0c Reviewed-by: Simon Hausmann --- tests/auto/qml/qqmltypeloader/data/trim_cache.qml | 8 ++++++ .../auto/qml/qqmltypeloader/tst_qqmltypeloader.cpp | 30 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/auto/qml/qqmltypeloader/data/trim_cache.qml (limited to 'tests/auto/qml') diff --git a/tests/auto/qml/qqmltypeloader/data/trim_cache.qml b/tests/auto/qml/qqmltypeloader/data/trim_cache.qml new file mode 100644 index 0000000000..74303977dd --- /dev/null +++ b/tests/auto/qml/qqmltypeloader/data/trim_cache.qml @@ -0,0 +1,8 @@ +import QtQuick 2.2 +Rectangle +{ + objectName: "dings" + width: 100 + height: 100 + color: "blue" +} diff --git a/tests/auto/qml/qqmltypeloader/tst_qqmltypeloader.cpp b/tests/auto/qml/qqmltypeloader/tst_qqmltypeloader.cpp index 4c5b1f7e63..7045c7cbd4 100644 --- a/tests/auto/qml/qqmltypeloader/tst_qqmltypeloader.cpp +++ b/tests/auto/qml/qqmltypeloader/tst_qqmltypeloader.cpp @@ -35,6 +35,9 @@ #include #include #include +#include +#include +#include #include "../../shared/util.h" class tst_QQMLTypeLoader : public QQmlDataTest @@ -44,6 +47,7 @@ class tst_QQMLTypeLoader : public QQmlDataTest private slots: void testLoadComplete(); void loadComponentSynchronously(); + void trimCache(); }; void tst_QQMLTypeLoader::testLoadComplete() @@ -73,6 +77,32 @@ void tst_QQMLTypeLoader::loadComponentSynchronously() QVERIFY(o); } +void tst_QQMLTypeLoader::trimCache() +{ + QQmlEngine engine; + QQmlTypeLoader &loader = QQmlEnginePrivate::get(&engine)->typeLoader; + for (int i = 0; i < 256; ++i) { + QUrl url = testFileUrl("trim_cache.qml"); + url.setQuery(QString::number(i)); + + QQmlTypeData *data = loader.getType(url); + if (i % 5 == 0) // keep references to some of them so that they aren't trimmed + data->compiledData()->addref(); + + data->release(); + } + + for (int i = 0; i < 256; ++i) { + QUrl url = testFileUrl("trim_cache.qml"); + url.setQuery(QString::number(i)); + if (i % 5 == 0) + QVERIFY(loader.isTypeLoaded(url)); + else if (i < 128) + QVERIFY(!loader.isTypeLoaded(url)); + // The cache is free to keep the others. + } +} + QTEST_MAIN(tst_QQMLTypeLoader) #include "tst_qqmltypeloader.moc" -- cgit v1.2.3