Skip to content

fix: reduce virtual address space usage to support 32-bit (386) architectures - #2321

Open
adityeah8969 wants to merge 2 commits into
dgraph-io:mainfrom
adityeah8969:fix/memory-leak-32-bit-machine
Open

fix: reduce virtual address space usage to support 32-bit (386) architectures#2321
adityeah8969 wants to merge 2 commits into
dgraph-io:mainfrom
adityeah8969:fix/memory-leak-32-bit-machine

Conversation

@adityeah8969

@adityeah8969 adityeah8969 commented Jul 19, 2026

Copy link
Copy Markdown

Description

Fixes: #2287

Badger's default configuration consumes >4 GB of virtual address space during write-heavy workloads, exceeding the ~3 GB user-space limit on 32-bit systems. This causes mmap failures with ENOMEM despite abundant physical RAM.

This PR makes two targeted changes to bring the working set under the 32-bit ceiling without affecting 64-bit behavior.

  1. Release WAL mmap for immutable memtables

Each memtable WAL is mmap'd at 2× MemTableSize (128 MB). During writes, multiple memtables queue in db.imm before the background flusher drains them — all holding their WAL mmap simultaneously.

  • Added releaseWAL() which munmaps the WAL data and closes the fd after a memtable becomes immutable. All reads go through the skiplist, so the mmap is dead weight.
  • Called wherever a memtable is pushed to db.imm: openMemTables, ensureRoomForWrite, the memory flush handler, and DropPrefix.
  • On 32-bit this frees ~128 MB per queued memtable (~500 MB in the repro case).
  1. 1× vlog mmap with pre-rotation

Vlog files were mmap'd at 2× their configured size. With a 4 MB vlog file, hundreds of files accumulate during a 2 GB workload, consuming 8 MB each (4 GB total).

  • Reduced vlog mmap from 2× to 1× ValueLogFileSize. The 2× was unused headroom.
  • Added a pre-emptive rotation check before each entry write. When an entry would straddle the boundary, the file is rotated first — avoiding the need for mremap-based dynamic growth, which can itself fail on 32-bit.
  • Extracted a shared rotateVlog helper used by both the pre-rotation check and the existing toDisk().

Checklist

  • Code compiles correctly and linting passes locally
  • Tests added for new functionality, or regression tests for bug fixes added as applicable

@adityeah8969
adityeah8969 requested a review from a team as a code owner July 19, 2026 10:09
@CLAassistant

CLAassistant commented Jul 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@matthewmcneely
matthewmcneely force-pushed the fix/memory-leak-32-bit-machine branch from ff53ba7 to ef1c40d Compare August 12, 2026 20:48
adityeah8969 and others added 2 commits August 24, 2026 01:38
On 32-bit systems each memtable WAL consumes 128 MB of virtual address space
(2× MemTableSize). During write-heavy workloads multiple memtables can queue
in db.imm before the background flusher processes them, exhausting the ~3 GB
user-space limit.

Add releaseWAL() which munmaps the WAL data and closes the fd after the
memtable becomes immutable — all reads go through the skiplist, so the
mmap'd region is dead weight. The OnClose callback is replaced with a
simple os.Remove since the file descriptor is already closed.

Called at four sites where memtables transition to immutable:
- openMemTables (recovery path)
- ensureRoomForWrite (write-path rotation)
- memory flush handler
- DropPrefix

Co-Authored-By: Claude <noreply@anthropic.com>
Two changes to prevent virtual address space exhaustion on 32-bit:

1. Reduce vlog file mmap from 2× to 1× ValueLogFileSize. The 2× multiplier
   was dead headroom — each 4 MB vlog file consumed 8 MB of address space,
   and with hundreds of files the total exceeded the 3 GB 32-bit limit.

2. Add a pre-emptive rotation check before each vlog entry write. When an
   entry would straddle the file boundary, rotate to a fresh file first so
   the entry lands cleanly. This avoids the need for dynamic mmap growth
   via Truncate/mremap, which can fail on 32-bit under address pressure.

   The rotation logic is extracted into a shared rotateVlog helper used by
   both the pre-rotation check and the existing toDisk().

   Giant entries that exceed the file size still fall through to the
   existing Truncate path.

Co-Authored-By: Claude <noreply@anthropic.com>
@adityeah8969
adityeah8969 force-pushed the fix/memory-leak-32-bit-machine branch from ef1c40d to b12f3c0 Compare August 23, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected memory limits on 386 runtime

2 participants