From 95bc29a76fc1f93eaabe5383d934644067dfc884 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Wed, 30 May 2018 10:55:17 +0200 Subject: [PATCH] Force release of free memory This should not be needed, but i can totally reproduce that without it top is still reporting lots of memory used by Okular when it's really not (heaptrack reports memory freed correctly) BUGS: 394834 --- OkularConfigureChecks.cmake | 4 ++++ config-okular.h.cmake | 3 +++ core/document.cpp | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/OkularConfigureChecks.cmake b/OkularConfigureChecks.cmake index 70fe8768c..4b99fee39 100644 --- a/OkularConfigureChecks.cmake +++ b/OkularConfigureChecks.cmake @@ -10,6 +10,10 @@ else (OKULAR_FORCE_DRM) set(_OKULAR_FORCE_DRM 0) endif (OKULAR_FORCE_DRM) +# Check whether malloc_trim(3) is supported. +include(CheckSymbolExists) +check_symbol_exists(malloc_trim "malloc.h" HAVE_MALLOC_TRIM) + # at the end, output the configuration configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config-okular.h.cmake diff --git a/config-okular.h.cmake b/config-okular.h.cmake index afa49afe8..0caea358a 100644 --- a/config-okular.h.cmake +++ b/config-okular.h.cmake @@ -4,3 +4,6 @@ /* Defines if the purpose framework is available */ #define PURPOSE_FOUND ${PURPOSE_FOUND} +/* Defines if the purpose framework is available */ +#cmakedefine01 HAVE_MALLOC_TRIM + diff --git a/core/document.cpp b/core/document.cpp index a70f84c89..084e88cd9 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -98,6 +98,10 @@ #include +#if HAVE_MALLOC_TRIM +#include "malloc.h" +#endif + using namespace Okular; struct AllocatedPixmap @@ -2724,6 +2728,13 @@ void Document::closeDocument() d->m_undoStack->clear(); d->m_docdataMigrationNeeded = false; + +#if HAVE_MALLOC_TRIM + // trim unused memory, glibc should do this but it seems it does not + // this can greatly decrease the [perceived] memory consumption of okular + // see: https://sourceware.org/bugzilla/show_bug.cgi?id=14827 + malloc_trim(0); +#endif } void Document::addObserver( DocumentObserver * pObserver )