aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Vrátil <dvratil@kde.org>2016-09-05 17:29:39 (GMT)
committerDaniel Vrátil <dvratil@kde.org>2016-09-05 17:37:53 (GMT)
commitd2a2fcb3eb7d286cb09ac1ae6642f9f245e1d904 (patch)
treea51bfc9c6e67dcaa64229aef8a776f0c7b52602f
parent593ca2f40dd456ad3b37a36877cf6ee5f9df7549 (diff)
Call malloc_trim() every 15 minutes to free more memory
In PIM we often have the problem with too many Items getting created temporarily and then when they are destroyed glibc will not return the memory back to kernel. The AkonadiCore library now calls malloc_trim() every 15 minutes to release this unused memory, keeping around up to 50MiB.
-rw-r--r--CMakeLists.txt3
-rw-r--r--config-akonadi.h.cmake2
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/braveheart.cpp66
4 files changed, 72 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9077003..070b7ac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,7 @@ include(KDECMakeSettings)
include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE)
include(CheckIncludeFiles)
include(ECMQtDeclareLoggingCategory)
+include(CheckSymbolExists)
include(AkonadiMacros)
@@ -88,6 +89,8 @@ else()
set(KF5Akonadi_DATA_DIR "${CMAKE_INSTALL_PREFIX}/${KDE_INSTALL_DATADIR_KF5}/akonadi")
endif()
+check_symbol_exists(malloc_trim "malloc.h" HAVE_MALLOC_TRIM)
+
############### Build Options ###############
option(AKONADI_BUILD_QSQLITE "Build the Sqlite backend." TRUE)
option(BUILD_TOOLS "Build and install tools for development and testing purposes." TRUE)
diff --git a/config-akonadi.h.cmake b/config-akonadi.h.cmake
index 211b09e..60ac9b0 100644
--- a/config-akonadi.h.cmake
+++ b/config-akonadi.h.cmake
@@ -5,4 +5,6 @@
#cmakedefine HAVE_UNISTD_H 1
+#cmakedefine HAVE_MALLOC_TRIM 1
+
#define AKONADI_DATABASE_BACKEND "@AKONADI_DATABASE_BACKEND@"
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index bb80e69..35d91d0 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -6,6 +6,7 @@ set(akonadicore_base_SRCS
asyncselectionhandler.cpp
attribute.cpp
attributefactory.cpp
+ braveheart.cpp
cachepolicy.cpp
changemediator_p.cpp
changenotification.cpp
diff --git a/src/core/braveheart.cpp b/src/core/braveheart.cpp
new file mode 100644
index 0000000..c455317
--- /dev/null
+++ b/src/core/braveheart.cpp
@@ -0,0 +1,66 @@
+/*
+ Copyright (c) 2016 Daniel Vrátil <dvratil@kde.org>
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Library General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
+
+ This library is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
+ License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
+*/
+
+#include <config-akonadi.h>
+
+#ifdef HAVE_MALLOC_TRIM
+
+#include <QCoreApplication>
+#include <QTimer>
+#include <QVariant>
+#include <QThread>
+
+#include <malloc.h>
+
+namespace Akonadi
+{
+
+class Braveheart
+{
+public:
+ explicit Braveheart()
+ {
+ qAddPreRoutine([]() {
+ if (!qApp->property("__Akonadi__Braveheart").isNull()) {
+ // One Scottish warrior is enough....
+ return;
+ }
+ auto freedom = new QTimer();
+ QObject::connect(freedom, &QTimer::timeout,
+ freedom, []() {
+ // They may take our lives, but they will never
+ // take our memory!
+ malloc_trim(50 * 1024 * 1024);
+ });
+ // Fight for freedom every 15 minutes
+ freedom->start(15 * 60 * 1000);
+ qApp->setProperty("__Akonadi__Braveheart", true);
+ });
+ }
+};
+
+namespace {
+
+Braveheart Wallace;
+
+}
+
+} // namespace Akonadi
+
+#endif // HAVE_MALLOC_TRIM