Skip to content

fix accounting + report real macOS memory pressure (-P)#11

Merged
aanogueira merged 1 commit into
mainfrom
andre.nogueira/fix-mem-accounting
Jul 6, 2026
Merged

fix accounting + report real macOS memory pressure (-P)#11
aanogueira merged 1 commit into
mainfrom
andre.nogueira/fix-mem-accounting

Conversation

@aanogueira

Copy link
Copy Markdown
Contributor

Summary

Fixes two memory-accounting bugs and adds an opt-in flag that surfaces the real macOS memory-pressure signal.

Bug fixes

  • shared no longer misreports the compressor. It was printing compressor_page_count in the shared column — semantically wrong (Linux shared = Shmem, not compressed anonymous memory). macOS exposes no cheap system-wide shared-memory counter, so we now report 0 (honest placeholder) rather than a misleading value.
  • speculative pages are now counted. speculative_count (read-ahead file cache) was never read, so it was missing from buff/cache and available. Including it tightens the total = used + free + buff/cache identity (was off ~0.5 GB, now ~0.4%).

New feature: -P / --pressure

macOS relieves memory pressure with an in-RAM compressor before it touches swap, so the Linux-style free/swap columns can look healthy while the machine is actually thrashing. -P prints the real kernel signal kern.memorystatus_vm_pressure_level (the same one Activity Monitor uses): normal / warning / critical, plus available %.

$ free -h -P
              total        used        free      shared  buff/cache   available
Mem:         32.0Gi      11.4Gi       7.4Gi          0B      13.4Gi      20.8Gi
Swap:            0B          0B          0B
Pressure: normal (64% available)

Default output stays Linux-parseable (flag is opt-in).

Validation

  • make clean under -Wall -Wextra -Werror -pedantic
  • clang-format --Werror clean
  • make test smoke passes
  • -P output cross-checked against sysctl kern.memorystatus_vm_pressure_level; column values cross-checked against vm_stat
  • README updated

Follow-up (not in this PR)

A genuine shared value would require summing shared footprint across all tasks (as top/libtop do) — filed as a possible enhancement.

🤖 Generated with Claude Code

…ting

- shared: stop reporting the memory compressor as "shared" (semantically
  wrong); macOS exposes no system-wide shared-memory counter, so report 0
- buff/cache & available: include speculative (read-ahead) pages that were
  previously omitted, tightening the total = used + free + buff/cache identity
- add -P/--pressure: prints the real kernel signal
  (kern.memorystatus_vm_pressure_level, the same one Activity Monitor uses),
  since macOS compresses in-RAM before swapping and the Linux-style columns
  cannot reveal pressure
- update README to match the new behavior

Signed-off-by: Andre Nogueira <aanogueira@protonmail.com>
@aanogueira aanogueira merged commit 07eccc0 into main Jul 6, 2026
1 check passed
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