[glibc] malloc: Reduce maximum arenas

Wilco Dijkstra via Glibc-cvs <[email protected]> Thu, 4 Jun 2026 14:27:27 +0000 (GMT)
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=93e6135904eec91f6aaf5500175e7593d260dbe7

commit 93e6135904eec91f6aaf5500175e7593d260dbe7
Author: Wilco Dijkstra <[email protected]>
Date:   Thu Apr 2 14:51:34 2026 +0000

    malloc: Reduce maximum arenas
    
    The default maximum arenas is 8 times the number of cores in a 64-bit system.
    Since modern CPUs have many cores and big servers have 256 cores, this results
    in excessive number of arenas, which wastes memory.  Limit the number of arenas
    to max (8, ncores) which is less extreme.  In the future the limit should be
    lowered further for large systems.
    
    Reviewed-by: Adhemerval Zanella  <[email protected]>

Diff:
---
 malloc/arena.c       | 13 ++++---------
 malloc/malloc.c      |  3 +--
 manual/memory.texi   |  7 ++-----
 manual/tunables.texi |  6 ++----
 4 files changed, 9 insertions(+), 20 deletions(-)

diff --git a/malloc/arena.c b/malloc/arena.c
index 023cb3ba06..1ed419a305 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -799,16 +799,11 @@ arena_get2 (size_t size, mstate avoid_arena)
         {
           if (mp_.arena_max != 0)
             narenas_limit = mp_.arena_max;
-          else if (narenas > mp_.arena_test)
+          else if (narenas >= mp_.arena_test)
             {
-              int n = __get_nprocs ();
-
-              if (n >= 1)
-                narenas_limit = NARENAS_FROM_NCORES (n);
-              else
-                /* We have no information about the system.  Assume two
-                   cores.  */
-                narenas_limit = NARENAS_FROM_NCORES (2);
+	      narenas_limit = __get_nprocs ();
+	      if (narenas_limit < mp_.arena_test)
+		narenas_limit = mp_.arena_test;
             }
         }
     repeat:;
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 6ce76cb4df..e121fad35a 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1650,8 +1650,7 @@ static struct malloc_par mp_ =
   .n_mmaps_max = DEFAULT_MMAP_MAX,
   .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
   .trim_threshold = DEFAULT_TRIM_THRESHOLD,
-#define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
-  .arena_test = NARENAS_FROM_NCORES (1),
+  .arena_test = sizeof (long) == 4 ? 2 : 8,
   .thp_mode = thp_mode_not_supported
 #if USE_TCACHE
   ,
diff --git a/manual/memory.texi b/manual/memory.texi
index f8b6be6ee7..744ea6ac64 100644
--- a/manual/memory.texi
+++ b/manual/memory.texi
@@ -1388,11 +1388,8 @@ environment variable @env{MALLOC_ARENA_TEST} to the desired value.
 This parameter sets the number of arenas to use regardless of the number of
 cores in the system.
 
-The default value of this tunable is @code{0}, meaning that the limit on the
-number of arenas is determined by the number of CPU cores online. For 32-bit
-systems the limit is twice the number of cores online and on 64-bit systems, it
-is eight times the number of cores online.  Note that the default value is not
-derived from the default value of M_ARENA_TEST and is computed independently.
+The default value of this tunable is @code{0}, meaning that the limit is the
+number of CPU cores online reported by get_nprocs().
 
 This parameter can also be set for the process at startup by setting the
 environment variable @env{MALLOC_ARENA_MAX} to the desired value.
diff --git a/manual/tunables.texi b/manual/tunables.texi
index 05f5e65a89..e0f141077b 100644
--- a/manual/tunables.texi
+++ b/manual/tunables.texi
@@ -220,10 +220,8 @@ identical in features.
 This tunable sets the number of arenas to use in a process regardless of the
 number of cores in the system.
 
-The default value of this tunable is @code{0}, meaning that the limit on the
-number of arenas is determined by the number of CPU cores online.  For 32-bit
-systems the limit is twice the number of cores online and on 64-bit systems, it
-is 8 times the number of cores online.
+The default value of this tunable is @code{0}, meaning that the limit is the
+number of CPU cores online reported by get_nprocs().
 @end deftp
 
 @deftp Tunable glibc.malloc.tcache_max