Skip to content

Implement RAM usage reporting#6

Open
heath-toby wants to merge 1 commit intotrypsynth:mainfrom
heath-toby:implement-ram-usage
Open

Implement RAM usage reporting#6
heath-toby wants to merge 1 commit intotrypsynth:mainfrom
heath-toby:implement-ram-usage

Conversation

@heath-toby
Copy link

Summary

  • Implements the previously empty ramUsage() function (Ctrl+Shift+2)
  • Reports physical memory usage using hs.host.vmStat(), calculating used memory as total minus reclaimable pages (free, inactive, speculative, purgeable)
  • Reports swap usage via sysctl vm.swapusage when swap is active; shows "0 bytes used" otherwise

Example output

  • No swap: "Physical: 9.86GB of 16.0GB used (61.6%). Swap: 0 bytes used."
  • With swap: "Physical: 14.2GB of 16.0GB used (88.75%). Swap: 1.5GB of 4.0GB used."

Approach and limitations

macOS does not expose the exact memory accounting used by Activity Monitor through public APIs. Activity Monitor uses private per-process APIs (task_info / phys_footprint) which capture IOKit/GPU memory allocations and per-process compressed memory attribution that aren't visible through vm_stat page categories.

This implementation uses hs.host.vmStat() (which wraps the kernel's vm_stat data) and defines "used memory" as total physical memory minus pages the system can instantly reclaim (free + inactive + speculative + purgeable). In testing, this tracks within ~1 GB of Activity Monitor's "Memory Used" figure on a 16 GB system.

Several alternative approaches were evaluated:

  • Summing active + wired + compressor pages: Under-counts by missing GPU/IOKit allocations
  • Parsing top -l 1 PhysMem output: Over-counts by including cached files as "used"
  • Current approach (total minus reclaimable): Best balance of accuracy and simplicity using only public APIs

Test plan

  • Reload Hammerspoon config and press Ctrl+Shift+2
  • Verify physical memory usage is announced and is reasonably close to Activity Monitor
  • Verify swap reports "0 bytes used" when no swap is active
  • Test on systems with active swap usage if possible

🤖 Generated with Claude Code

Uses hs.host.vmStat() to calculate physical memory usage by subtracting
reclaimable pages (free, inactive, speculative, purgeable) from total
memory. Also reports swap usage via sysctl when swap is active.

Note: macOS does not expose the exact memory accounting used by Activity
Monitor through public APIs, so this approximation may differ by up to
~1 GB. The discrepancy comes from IOKit/GPU allocations and per-process
memory attribution that require private Apple frameworks to capture.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@trypsynth
Copy link
Owner

Oh hey there, long time no chat, old friend. I'll give this a review soon, I'll pull out my mac and test it. Thanks for stepping up to this challenge, my solution was going to be to write some objective-c to do it like psutil

@heath-toby
Copy link
Author

heath-toby commented Mar 3, 2026 via email

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.

2 participants