diff --git a/src/app/main.cpp b/src/app/main.cpp index c56f22b642d..4302e9018c2 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -69,6 +69,10 @@ #include #endif +#ifdef Q_OS_LINUX +#include +#endif + using namespace ExtensionSystem; enum { OptionIndent = 4, DescriptionIndent = 34 }; @@ -704,5 +708,32 @@ int main(int argc, char **argv) // shutdown plugin manager on the exit QObject::connect(&app, &QCoreApplication::aboutToQuit, &pluginManager, &PluginManager::shutdown); +#ifdef Q_OS_LINUX + class MemoryTrimmer : public QObject + { + public: + MemoryTrimmer() + { + m_trimTimer.setSingleShot(true); + m_trimTimer.setInterval(60000); + // glibc may not actually free memory in free(). + connect(&m_trimTimer, &QTimer::timeout, this, [] { malloc_trim(0); }); + } + + bool eventFilter(QObject *, QEvent *e) override + { + if ((e->type() == QEvent::MouseButtonPress || e->type() == QEvent::KeyPress) + && !m_trimTimer.isActive()) { + m_trimTimer.start(); + } + return false; + } + + QTimer m_trimTimer; + }; + MemoryTrimmer trimmer; + app.installEventFilter(&trimmer); +#endif + return restarter.restartOrExit(app.exec()); }