Skip to content

Each arena commits at least 2 MiB of metadata on Windows #3068

Todo

Description

@mat-gas

Version: 5.4.0 (7a34f18). The same rounding exists since at least 5.3.0, which used HUGEPAGE_CEILING "regardless of opt_metadata_thp".
Build: clang-cl (MSVC ABI) through configure, default options, metadata_thp disabled (default).
Test machine: Windows x64, 8 logical CPUs, one processor group.

Summary

On Windows, creating an arena raises the process's commit charge (PrivateUsage) by 2.00 MiB, while stats.metadata grows by about 0.15 MiB. Every arena gets its own base, the first base block is rounded up to 2 MiB, and on Windows the whole block is committed when it is mapped.

Code path

  1. arena_new creates a base for every arena except arena 0: src/arena.c#L1477-L1490
  2. base_new → base_block_alloc: src/base.c#L478-L495
  3. base_block_alloc sizes the block with base_block_size_ceil (src/base.c#L370-L398). With metadata_thp disabled, that rounds up to BASE_BLOCK_MIN_ALIGN (src/base.c#L357-L362), which is 2 MiB (include/jemalloc/internal/base.h#L13).
  4. base_map maps the block with commit = true: src/base.c#L51-L68. Then extent_alloc_mmap → pages_map → os_vm_reserve: src/extent_mmap.c#L21-L24, src/pages.c#L71-L92
  5. On Windows os_overcommits is always false (os/windows/overcommit.h#L16). So os_vm_reserve calls VirtualAlloc(MEM_RESERVE | MEM_COMMIT) on the whole block: os/windows/vm.h#L8-L23

On Linux with vm.overcommit_memory 0 or 1 (os/linux/overcommit.h#L103-L108), and on macOS, the same block is only reserved, and untouched pages cost nothing.

Measurements

A small program statically linked against 5.4.0 creates arenas and reports the change in PrivateUsage (from GetProcessMemoryInfo) and in stats.metadata. Each thread allocates once with tcache:false; thread stacks are excluded from the baseline.

Scenario Arenas created PrivateUsage Per arena stats.metadata
arenas.create × 64 64 +128.2 MiB 2.00 MiB +9.54 MiB
32 threads, default narenas (32) 31 +62.2 MiB 2.01 MiB +3.30 MiB
64 threads, percpu_arena:percpu, thread i pinned to CPU i mod 8 7 +14.1 MiB 2.01 MiB +0.75 MiB
8 threads, narenas:8 7 +14.1 MiB 2.01 MiB +0.75 MiB

In production, an application using percpu_arena:percpu committed 68 MiB of metadata on a 28-core host and 52 MiB on a 20-core host. stats.metadata was 17.5 MiB on the 28-core host. That fits 2 MiB × arenas + about 12 MiB. With the default narenas (4 × CPUs), a 28-core host could reach about 224 MiB.

Proposed fix

Use a smaller minimum base block on Windows. Blocks stay committed in full when mapped, but are only rounded up to 64 KiB. That is also Windows' allocation granularity, so reservations need no realignment, and stats.mapped stays equal to what is committed.

--- a/include/jemalloc/internal/base.h
+++ b/include/jemalloc/internal/base.h
@@ -10,7 +10,15 @@
  * Alignment when THP is not enabled.  Set to constant 2M in case the HUGEPAGE
  * value is unexpected high (which would cause VM over-reservation).
  */
-#define BASE_BLOCK_MIN_ALIGN ((size_t)2 << 20)
+#ifdef _WIN32
+/*
+ * Windows commits a block in full when mapping it, so keep blocks small: 64 KiB
+ * is also its allocation granularity, so reservations need no realignment.
+ */
+#  define BASE_BLOCK_MIN_ALIGN ((size_t)64 << 10)
+#else
+#  define BASE_BLOCK_MIN_ALIGN ((size_t)2 << 20)
+#endif

Same Windows machine, with this change:

Scenario 5.4.0 With the fix Per arena with the fix
arenas.create × 64 +128.2 MiB +15.8 MiB 0.25 MiB
31 automatic arenas +62.2 MiB +5.9 MiB 0.19 MiB
7 per-CPU arenas +14.1 MiB +1.3 MiB 0.19 MiB

We also ran it on Linux, with the constant set to 64 KiB there and os_overcommits forced to false at boot (a test-only change), so that blocks are committed in full as on Windows.

  • Unit and integration tests: they pass, except test/unit/hpa, which segfaults with that override even without the change.
  • After a burst of 60,000 one-page slabs in one arena, committed-but-unused metadata dropped from 17.4 MiB to 1.0 MiB. In runs where the burst needed 2 MiB more metadata, it dropped from 15.4 MiB to 3.0 MiB.

Reproducing

repro.c

Build the program above as a console executable, statically linked against jemalloc (/MT, -DJEMALLOC_EXPORT=, plus psapi.lib), once against 5.4.0 and once with the fix. Then run:

arena-repro.exe arenas 64
arena-repro.exe threads
arena-repro.exe threads 64 "percpu_arena:percpu,tcache:false"

Compare the private usage column (the change in PrivateUsage) with metadata in use (the change in stats.metadata).

arenas 64: 64 arenas created with arenas.create. Before:

jemalloc 5.4.0-0-g7a34f18502e7b222724097cdcd499b437d189acc
logical processors: 8 in 1 group(s), 8 seen by jemalloc
config: "", opt.narenas 32, opt.percpu_arena disabled, opt.metadata_thp disabled

creating 64 manual arenas (arenas.create), 1 arena(s) already initialized
  arenas     private usage (MiB)        per arena   jemalloc mapped (MiB)   metadata in use (MiB)
       1                    +2.0         2.00 MiB                    +2.0                   +0.15
       2                    +4.0         2.00 MiB                    +4.0                   +0.30
       4                    +8.0         2.00 MiB                    +8.0                   +0.60
       8                   +16.0         2.00 MiB                   +16.0                   +1.19
      16                   +32.1         2.00 MiB                   +32.0                   +2.39
      32                   +64.1         2.00 MiB                   +64.0                   +4.77
      64                  +128.2         2.00 MiB                  +128.0                   +9.54

After:

  arenas     private usage (MiB)        per arena   jemalloc mapped (MiB)   metadata in use (MiB)
       1                    +0.2         0.19 MiB                    +0.2                   +0.15
       2                    +0.4         0.19 MiB                    +0.4                   +0.30
       4                    +0.8         0.19 MiB                    +0.8                   +0.60
       8                    +2.5         0.31 MiB                    +2.5                   +1.19
      16                    +4.0         0.25 MiB                    +4.0                   +2.39
      32                    +8.3         0.26 MiB                    +8.2                   +4.78
      64                   +15.8         0.25 MiB                   +15.8                   +9.55

threads: as many threads as the default narenas (32), each allocating once with tcache:false. Before:

config: "tcache:false", opt.narenas 32, opt.percpu_arena disabled, opt.metadata_thp disabled

32 threads allocating once each, 1 arena(s) already initialized
  arenas     private usage (MiB)        per arena   jemalloc mapped (MiB)   metadata in use (MiB)
      31                   +62.2         2.01 MiB                   +62.1                   +3.30

After:

      31                    +5.9         0.19 MiB                    +5.9                   +3.31

threads 64 "percpu_arena:percpu,tcache:false": 64 threads, thread i pinned to CPU i mod 8. Before:

config: "percpu_arena:percpu,tcache:false", opt.narenas 8, opt.percpu_arena percpu, opt.metadata_thp disabled

64 threads allocating once each, 1 arena(s) already initialized
  arenas     private usage (MiB)        per arena   jemalloc mapped (MiB)   metadata in use (MiB)
       7                   +14.1         2.01 MiB                   +14.0                   +0.75

After:

       7                    +1.3         0.19 MiB                    +1.3                   +0.75

Before the fix, private usage grows by 2 MiB per arena whatever the arena's metadata needs. After it, private usage follows stats.metadata closely, with each block rounded up to 64 KiB.

Activity

  1. guangli-dai commented on Nov 24, 2026

    @guangli-dai
    Contributor

    Thank you for the detailed report. It looks real and the reproduction is very helpful. I would prefer to solve it combining with our latest OS layer, which is the same idea you propose but just more compatible. I will have a PR for this soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions