[gnome-settings-daemon/wip/chergert/mem-reduce: 4/4] datetime: use malloc_trim() to release resources back to OS



commit 7e351b3da75252c0085ec8eaccecf56fe9f3ca73
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 10 20:12:02 2020 -0700

    datetime: use malloc_trim() to release resources back to OS
    
    We allocate a lot of memory while building the timezone database from
    libgweather all to have that put into the GSlice allocation queues we'll
    barely if ever touch again.
    
    By forcing things to use malloc, we can use malloc_trim() on supported
    GLibc systems to madvise() the kernel it can take the memory back.
    
    With the previous patches and this, gsd-datetime drops from about 9MB down
    to just under 3MB. We could probably go even further if we change
    libgweather a bit, but this gets it small enough for me.
    
    However, there are other processes (such as evolution-calendar-factory)
    that would benefit from a more generic solution.
    
    It's also very likely that we would want to just make gsd-datetime
    activatable through external events so the process can go away entirely.

 meson.build                              |  9 +++++++++
 plugins/datetime/gsd-datetime.service.in |  2 ++
 plugins/datetime/weather-tz.c            | 12 ++++++++++++
 3 files changed, 23 insertions(+)
---
diff --git a/meson.build b/meson.build
index cd8616cb..578ae07a 100644
--- a/meson.build
+++ b/meson.build
@@ -219,6 +219,15 @@ if enable_network_manager
 endif
 config_h.set10('HAVE_NETWORK_MANAGER', enable_network_manager)
 
+# check for malloc_trim(0)
+malloc_trim_code = '''
+#include <malloc.h>
+void func() { malloc_trim(0); }
+'''
+if cc.compiles(malloc_trim_code, name: 'malloc_trim')
+  config_h.set10('HAVE_MALLOC_TRIM', 1)
+endif
+
 gnome = import('gnome')
 i18n = import('i18n')
 pkg = import('pkgconfig')
diff --git a/plugins/datetime/gsd-datetime.service.in b/plugins/datetime/gsd-datetime.service.in
index bd4692b8..0607087f 100644
--- a/plugins/datetime/gsd-datetime.service.in
+++ b/plugins/datetime/gsd-datetime.service.in
@@ -17,3 +17,5 @@ ExecStart=@libexecdir@/gsd-datetime
 Restart=on-failure
 BusName=@plugin_dbus_name@
 TimeoutStopSec=5
+# gsd-datetime will use malloc_trim()
+Environment=G_SLICE=always-malloc
diff --git a/plugins/datetime/weather-tz.c b/plugins/datetime/weather-tz.c
index f4afdaca..47fa6d31 100644
--- a/plugins/datetime/weather-tz.c
+++ b/plugins/datetime/weather-tz.c
@@ -24,6 +24,9 @@
 #endif
 
 #include <dlfcn.h>
+#ifdef HAVE_MALLOC_TRIM
+# include <malloc.h>
+#endif
 
 #include "weather-tz.h"
 #include "tz.h"
@@ -112,6 +115,15 @@ release_world (void)
        if (release_world_func != NULL) {
                g_debug ("Releasing Locations.xml data");
                release_world_func ();
+
+#ifdef HAVE_MALLOC_TRIM
+               /* A lot of the memory we just allocated won't be released back
+                * to the operating system unless malloc is told to release it.
+                * This also pretty much relies on G_SLICE=always-malloc so
+                * that libgweather does not allocate everything with gslice.
+                */
+               malloc_trim (0);
+#endif
        }
        else {
                g_debug ("Cannot locate symbol _gweather_location_reset_world()");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]