Skip to content

balloon: use MADV_FREE to reclaim memory on macOS#703

Open
pftbest wants to merge 1 commit into
containers:mainfrom
pftbest:balloon-madv-free
Open

balloon: use MADV_FREE to reclaim memory on macOS#703
pftbest wants to merge 1 commit into
containers:mainfrom
pftbest:balloon-madv-free

Conversation

@pftbest
Copy link
Copy Markdown
Contributor

@pftbest pftbest commented May 31, 2026

The current implementation of balloon on macOS is not working correctly, because it is using the wrong flag for madvise.

The MADV_DONTNEED flag is no-op on macOS because it preserves the data, so the OS cannot discard the pages. MADV_FREE flag actually frees the memory under pressure, which is what we need for balloon.

How to test this:

  1. Inside the VM fill the whole memory with incompressible data:
mount -t tmpfs -o size=90% none /tmp
dd if=/dev/urandom of=/tmp/data bs=1M
  1. Observe on the host the memory pressure is high (run some background apps to make it even higher if needed).
  2. Remove the data in the VM
rm /tmp/data
  1. Observe any change in memory pressure

With MADV_DONTNEED flag nothing changes, balloon is not working:
Screenshot 2026-05-31 at 10 45 49

With MADV_FREE the pressure drops right away:
Screenshot 2026-05-31 at 10 52 53

Notably the per process memory usage in Activity Monitor still reports 8GB in both cases, however this numbers are not accurate, so the only way to test is to observe the pressure and total RAM usage.

The MADV_DONTNEED flag is no-op on macOS because it preserves the data,
so the OS cannot discard the pages. MADV_FREE flag actually frees
the memory under pressure, which is what we need for balloon.

Signed-off-by: Vadzim Dambrouski <pftbest@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant