Commit d2a2fcb3 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

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.
parent 593ca2f4
......@@ -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)
......
......@@ -5,4 +5,6 @@
#cmakedefine HAVE_UNISTD_H 1
#cmakedefine HAVE_MALLOC_TRIM 1
#define AKONADI_DATABASE_BACKEND "@AKONADI_DATABASE_BACKEND@"
......@@ -6,6 +6,7 @@ set(akonadicore_base_SRCS
asyncselectionhandler.cpp
attribute.cpp
attributefactory.cpp
braveheart.cpp
cachepolicy.cpp
changemediator_p.cpp
changenotification.cpp
......
/*
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment