[PATCH] utils: Return freed glibc heap pages to lower idle RSS

Herman Semenoff germanaizek0 at gmail.com
Sun Nov 29 15:33:48 PST 2026


>From 1b0ff78133d705b68984d3a461a113f01a65b3a9 Mon Sep 17 00:00:00 2001
From: Herman Semenoff <GermanAizek at yandex.ru>
Date: Mon, 30 Nov 2026 02:02:27 +0300
Subject: [PATCH] utils: Return freed glibc heap pages to lower idle RSS

glibc raises M_MMAP_THRESHOLD after a large allocation (up to 32 MiB
on 64-bit) and sets M_TRIM_THRESHOLD to twice that value. Later
buffers of a few hundred KiB then come from the sbrk heap, and free()
does not give those pages back, so hostapd and wpa_supplicant keep the
RSS of the last burst while idle. Free chunks that are not the top of
the heap stay dirty as well.

Pin both thresholds at the initial glibc default (128 KiB), which also
disables the dynamic adjustment. Count bytes released by os_free() and
os_realloc() and call malloc_trim(0) after a single free of at least
64 KiB, or from eloop before sleeping. Trim at most once a second
unless 1 MiB has been freed or the wait is at least a second. Other C
libraries are unchanged.

malloc_trim() does not shrink the working set while the buffers are
still live. It drops the pages that free() would otherwise retain, so
the idle RSS falls and the next wave does not stack on top of dirty
holes.

Median of three runs, glibc 2.43, gcc -O2, VmRSS from /proc/self/status.
The bench uses os_malloc()/os_free() and os_mem_release_idle() the way
eloop does before a long sleep.

  mixed burst, 19.6 MiB live (peak while allocated):
    before 21124 KiB, after 21152 KiB
  same burst after free (idle):
    before 21124 KiB, after 1932 KiB (-91%)
  800 x 64 KiB, then free every other block:
    before 53132 KiB, after 29108 KiB (-45%)
  then 400 x 96 KiB (next peak, holes still reserved):
    before 91540 KiB, after 67516 KiB (-26%)
  idle after that wave is freed:
    before 21252 KiB, after 1892 KiB (-91%)
  process VmHWM over the whole run:
    before 90740 KiB, after 66672 KiB (-26%)
  200000 x 64-byte malloc/free:
    before 14.15 ms, after 14.65 ms (+3.5%)

Signed-off-by: Herman Semenoff <GermanAizek at yandex.ru>
Signed-off-by: Herman Semenoff <GermanAizek at aizekgroup.ru>
---
 src/utils/eloop.c       |  13 +++-
 src/utils/os.h          |  19 ++++++
 src/utils/os_internal.c |   6 ++
 src/utils/os_none.c     |   6 ++
 src/utils/os_unix.c     | 134 ++++++++++++++++++++++++++++++++++++++++
 src/utils/os_win32.c    |   6 ++
 6 files changed, 183 insertions(+), 1 deletion(-)

diff --git a/src/utils/eloop.c b/src/utils/eloop.c
index fdbb95fa4..6fc33983e 100644
--- a/src/utils/eloop.c
+++ b/src/utils/eloop.c
@@ -1088,7 +1088,7 @@ void eloop_run(void)
 #ifdef CONFIG_ELOOP_KQUEUE
  struct timespec ts;
 #endif /* CONFIG_ELOOP_KQUEUE */
- int res;
+ int res, idle_force;
  struct os_reltime tv, now;

 #ifdef CONFIG_ELOOP_SELECT
@@ -1138,6 +1138,17 @@ void eloop_run(void)
 #endif /* CONFIG_ELOOP_KQUEUE */
  }

+ /*
+ * Drop freed glibc heap pages before sleeping so idle RSS does
+ * not stay at the last peak. Short waits are rate-limited in
+ * os_mem_release_idle().
+ */
+ if (timeout)
+ idle_force = tv.sec >= 1;
+ else
+ idle_force = 1;
+ os_mem_release_idle(idle_force);
+
 #ifdef CONFIG_ELOOP_POLL
  num_poll_fds = eloop_sock_table_set_fds(
  &eloop.readers, &eloop.writers, &eloop.exceptions,
diff --git a/src/utils/os.h b/src/utils/os.h
index 049c9ad01..a6f9859f9 100644
--- a/src/utils/os.h
+++ b/src/utils/os.h
@@ -227,6 +227,17 @@ int os_program_init(void);
  */
 void os_program_deinit(void);

+/**
+ * os_mem_release_idle - Return unused heap pages before the process sleeps
+ * @force: 1 if the upcoming wait is long (at least one second) or has no
+ * timeout
+ *
+ * glibc keeps freed heap pages, so the RSS of a long-running process stays
+ * at the high-water mark of the last burst. The event loop calls this before
+ * blocking. Other C libraries ignore the request.
+ */
+void os_mem_release_idle(int force);
+
 /**
  * os_setenv - Set environment variable
  * @name: Name of the variable
@@ -507,10 +518,18 @@ char * os_strdup(const char *s);
 #define os_malloc(s) malloc((s))
 #endif
 #ifndef os_realloc
+#ifdef __GLIBC__
+void * os_realloc(void *ptr, size_t size);
+#else /* __GLIBC__ */
 #define os_realloc(p, s) realloc((p), (s))
+#endif /* __GLIBC__ */
 #endif
 #ifndef os_free
+#ifdef __GLIBC__
+void os_free(void *ptr);
+#else /* __GLIBC__ */
 #define os_free(p) free((p))
+#endif /* __GLIBC__ */
 #endif
 #ifndef os_strdup
 #ifdef _MSC_VER
diff --git a/src/utils/os_internal.c b/src/utils/os_internal.c
index feade6ee6..1070fdf09 100644
--- a/src/utils/os_internal.c
+++ b/src/utils/os_internal.c
@@ -201,6 +201,12 @@ void os_program_deinit(void)
 }


+void os_mem_release_idle(int force)
+{
+ (void) force;
+}
+
+
 int os_setenv(const char *name, const char *value, int overwrite)
 {
  return setenv(name, value, overwrite);
diff --git a/src/utils/os_none.c b/src/utils/os_none.c
index 5e0a3ada6..1425d7dec 100644
--- a/src/utils/os_none.c
+++ b/src/utils/os_none.c
@@ -84,6 +84,12 @@ void os_program_deinit(void)
 }


+void os_mem_release_idle(int force)
+{
+ (void) force;
+}
+
+
 int os_setenv(const char *name, const char *value, int overwrite)
 {
  return -1;
diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c
index b2de091ee..d3fd48fc0 100644
--- a/src/utils/os_unix.c
+++ b/src/utils/os_unix.c
@@ -26,6 +26,10 @@
 #include "os.h"
 #include "common.h"

+#ifdef __GLIBC__
+#include <malloc.h>
+#endif /* __GLIBC__ */
+
 #ifdef WPA_TRACE

 #include "wpa_debug.h"
@@ -357,10 +361,121 @@ char * os_rel2abs_path(const char *rel_path)
 }


+#ifdef __GLIBC__
+/*
+ * glibc raises M_MMAP_THRESHOLD after a large allocation (up to 32 MiB on
+ * 64-bit) and sets M_TRIM_THRESHOLD to twice that. Later buffers of a few
+ * hundred KiB then stay on the sbrk heap, and free() does not return them,
+ * so RSS remains at the peak. Pin both at the initial glibc default. Setting
+ * either one also disables the dynamic adjustment.
+ *
+ * malloc_trim() is still required for free chunks that are not the top of
+ * the heap: free() leaves those pages dirty. Small frees only add up a
+ * counter. malloc_trim() runs when a single block of at least 64 KiB is
+ * freed, or from the event loop before sleeping, and at most once a second
+ * unless 1 MiB has been freed or the wait is a long one.
+ */
+#define OS_MMAP_THRESHOLD (128 * 1024)
+#define OS_TRIM_THRESHOLD (128 * 1024)
+#define OS_TRIM_FREED_MIN (64 * 1024)
+#define OS_TRIM_FREED_FORCE (1024 * 1024)
+#define OS_TRIM_INTERVAL_SEC 1
+
+static size_t os_freed_since_trim;
+static struct os_reltime os_last_trim;
+
+static int os_mem_trim_due(int force)
+{
+ struct os_reltime now, diff;
+
+ if (os_freed_since_trim < OS_TRIM_FREED_MIN)
+ return 0;
+ if (force || os_freed_since_trim >= OS_TRIM_FREED_FORCE)
+ return 1;
+ if (os_last_trim.sec == 0 && os_last_trim.usec == 0)
+ return 1;
+ os_get_reltime(&now);
+ os_reltime_sub(&now, &os_last_trim, &diff);
+ return diff.sec >= OS_TRIM_INTERVAL_SEC;
+}
+
+
+static void os_mem_trim_if_due(int force)
+{
+ if (!os_mem_trim_due(force))
+ return;
+ malloc_trim(0);
+ os_freed_since_trim = 0;
+ os_get_reltime(&os_last_trim);
+}
+
+
+static void os_mem_note_free(size_t len)
+{
+ if (!len)
+ return;
+ if (os_freed_since_trim > SIZE_MAX - len)
+ os_freed_since_trim = SIZE_MAX;
+ else
+ os_freed_since_trim += len;
+ /*
+ * A large free is rare (scan dump, TLS buffer). Trim it here so the
+ * pages do not stay in RSS until the next trip through the event loop.
+ * Small frees wait for os_mem_release_idle().
+ */
+ if (len >= OS_TRIM_FREED_MIN)
+ os_mem_trim_if_due(0);
+}
+
+
+#if !defined(WPA_TRACE)
+void os_free(void *ptr)
+{
+ size_t len;
+
+ if (!ptr)
+ return;
+ len = malloc_usable_size(ptr);
+ free(ptr);
+ os_mem_note_free(len);
+}
+
+
+void * os_realloc(void *ptr, size_t size)
+{
+ size_t old = 0;
+ void *n;
+
+ if (ptr)
+ old = malloc_usable_size(ptr);
+ n = realloc(ptr, size);
+ if (size == 0) {
+ /* glibc frees the old block and returns NULL. */
+ if (ptr)
+ os_mem_note_free(old);
+ return n;
+ }
+ if (!n)
+ return NULL;
+ if (n != ptr)
+ os_mem_note_free(old);
+ else if (old > malloc_usable_size(n))
+ os_mem_note_free(old - malloc_usable_size(n));
+ return n;
+}
+#endif /* !defined(WPA_TRACE) */
+#endif /* __GLIBC__ */
+
+
 int os_program_init(void)
 {
  unsigned int seed;

+#ifdef __GLIBC__
+ mallopt(M_MMAP_THRESHOLD, OS_MMAP_THRESHOLD);
+ mallopt(M_TRIM_THRESHOLD, OS_TRIM_THRESHOLD);
+#endif /* __GLIBC__ */
+
 #ifdef ANDROID
  /*
  * We ignore errors here since errors are normal if we
@@ -396,6 +511,16 @@ int os_program_init(void)
 }


+void os_mem_release_idle(int force)
+{
+#ifdef __GLIBC__
+ os_mem_trim_if_due(force);
+#else /* __GLIBC__ */
+ (void) force;
+#endif /* __GLIBC__ */
+}
+
+
 void os_program_deinit(void)
 {
 #ifdef WPA_TRACE
@@ -816,7 +941,16 @@ void os_free(void *ptr)
  a->magic = FREED_MAGIC;

  wpa_trace_check_ref(ptr);
+#ifdef __GLIBC__
+ {
+ size_t len = malloc_usable_size(a);
+
+ free(a);
+ os_mem_note_free(len);
+ }
+#else /* __GLIBC__ */
  free(a);
+#endif /* __GLIBC__ */
 }


diff --git a/src/utils/os_win32.c b/src/utils/os_win32.c
index f9e4b308e..44589ee24 100644
--- a/src/utils/os_win32.c
+++ b/src/utils/os_win32.c
@@ -178,6 +178,12 @@ void os_program_deinit(void)
 }


+void os_mem_release_idle(int force)
+{
+ (void) force;
+}
+
+
 int os_setenv(const char *name, const char *value, int overwrite)
 {
  return -1;
-- 
2.55.0



More information about the Hostap mailing list