Skip to content

darwin: exclude inactive and speculative pages from used memory - #1976

Merged
BenBE merged 1 commit into
htop-dev:mainfrom
natoscott:fix/macos-memory-used-calculation
Jun 1, 2026
Merged

darwin: exclude inactive and speculative pages from used memory#1976
BenBE merged 1 commit into
htop-dev:mainfrom
natoscott:fix/macos-memory-used-calculation

Conversation

@natoscott

Copy link
Copy Markdown
Member

Inactive and speculative pages are reclaimable caches, not actively used memory. Counting them as "used" inflates the displayed value far beyond what Activity Monitor reports, especially on high-RAM Apple Silicon systems (e.g. 49G vs 9G on a 192GB M2 Ultra).

Both classes already had countsAsCache=true; this change sets countsAsUsed=false so they no longer contribute to the "used" total. They remain visible when "Show cached memory" is enabled.

Resolves: #1975

@SuhasSrinivasan

SuhasSrinivasan commented May 1, 2026

Copy link
Copy Markdown

@natoscott
Sorry, the fix does not seem to be working in the high-memory Mac.
It appears to be reporting Wired Memory: 2.55G/192G.
Please see below

$ vm_stat | perl -ne '/page size of (\d+)/ and $size=$1; /Pages\s+([^:]+)[^\d]+(\d+)/ and printf("%-16s % 16.2f Mi\n", "$1:", $2 * $size / 1048576);'
free:                      220.58 Mi
active:                  11399.62 Mi
inactive:               181134.55 Mi
speculative:               104.58 Mi
throttled:                   0.00 Mi
wired down:               2538.06 Mi
purgeable:                  84.17 Mi
copy-on-write:          415799.92 Mi
zero filled:           1984858.75 Mi
reactivated:              1366.33 Mi
purged:                  61407.42 Mi
stored in compressor:           243.97 Mi
occupied by compressor:            68.73 Mi

@natoscott
natoscott force-pushed the fix/macos-memory-used-calculation branch from e906263 to 9dc8009 Compare May 18, 2026 01:30
@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 172555d2-aed9-474a-98cd-39d47b498c0a

📥 Commits

Reviewing files that changed from the base of the PR and between 9dc8009 and af53de4.

📒 Files selected for processing (2)
  • configure.ac
  • darwin/Platform.c

📝 Walkthrough

Summary

Surgically fixes macOS memory accounting that inflated "used" memory on high‑RAM Apple Silicon systems by reclassifying page types and switching to the correct page-count source for anonymous/app pages.

Changes

  • configure.ac: add autoconf check for struct vm_statistics64.internal_page_count (AC_CHECK_MEMBERS).
  • darwin/Platform.c:
    • Memory classes: set speculative and inactive to countsAsUsed=false while retaining countsAsCache=true (they remain visible when "Show cached memory" is enabled).
    • Platform_setMemoryValues: prefer vm->internal_page_count (when available) and compute Active = page_K * saturatingSub(internal_page_count, vm->purgeable_count). Inactive and speculative are nonzero only when showCachedMemory is enabled. Fallback to vm->active_count if internal_page_count is unavailable.

Assessment

  • Implementation is focused and minimal: only a build-time availability probe and the Darwin memory-class/metric computations were changed.
  • Logic aligns with Activity Monitor semantics (anonymous/app pages counted as active/app memory; inactive/speculative treated as cache).
  • Backward compatibility preserved via fallback when internal_page_count is missing.
  • Commit is cohesive and scoped; changes are small and reviewable.

Note

Reviewer feedback indicates a report where large inactive counts still appear and displayed wired/used values remained unexpected on a high‑memory Mac; follow-up testing on affected hardware is recommended.

Walkthrough

This PR updates macOS memory reporting: configure.ac now detects vm_statistics64.internal_page_count; Platform.c marks speculative and inactive pages as cache-only (countsAsUsed=false) and selects internal_page_count (or active_count fallback). Memory-class calculations for ACTIVE now use page_K * saturatingSub(internal_page_count, vm->purgeable_count); INACTIVE and SPECULATIVE are tied to showCachedMemory and PURGEABLE uses vm->purgeable_count when shown.

Assessment against linked issues

Objective Addressed Explanation
Fix incorrect memory usage reporting on macOS (#1975)
Use correct VM statistics field instead of external_page_count (#1975)
Adjust memory class accounting for speculative/inactive pages (#1975)

Poem

Where internal counts replace a ghostly tide,
Active trimmed by purgeable on the side,
Speculative and inactive step out of use,
Numbers settle true, no more obtuse,
Quiet metrics sing: the meter’s right.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2ad74dcb-56e9-456e-ad5b-b4409d26c982

📥 Commits

Reviewing files that changed from the base of the PR and between b7f9df9 and 9dc8009.

📒 Files selected for processing (2)
  • configure.ac
  • darwin/Platform.c

Comment thread darwin/Platform.c Outdated
Two issues caused htop to massively overreport memory usage on
high-RAM Apple Silicon systems (e.g. 49G vs 9G on a 192GB M2 Ultra):

1. Inactive and speculative pages were counted as used memory.
   These are reclaimable caches and should only appear when Show
   cached memory is enabled.

2. The active (app memory) value was computed by subtracting
   external_page_count from active_count.  external_page_count
   counts all file-backed pages across active, inactive and
   speculative lists, so on machines with large inactive lists
   it exceeds active_count and the saturatingSub clamp produces
   zero, leaving only wired memory in the total.

   Use internal_page_count (anonymous pages) directly instead,
   matching Activity Monitors App Memory definition:
     app_memory = internal_page_count - purgeable_count

Closes: htop-dev#1975

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@natoscott
natoscott force-pushed the fix/macos-memory-used-calculation branch from 9dc8009 to af53de4 Compare May 18, 2026 01:49
@SuhasSrinivasan

Copy link
Copy Markdown

Thank you, tested and confirming that new fix af53de4 reports as 15.1 G / 192 G.

htop dropped from the buggy 48 G down to 15.1 G.

@BenBE
BenBE merged commit 3a00221 into htop-dev:main Jun 1, 2026
21 checks passed
@BenBE BenBE added bug 🐛 Something isn't working MacOS 🍏 MacOS / Darwin related issues labels Jun 1, 2026
@BenBE BenBE added this to the 3.6.0 milestone Jun 1, 2026
@BenBE BenBE modified the milestones: 3.6.0, 3.5.2 Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐛 Something isn't working MacOS 🍏 MacOS / Darwin related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

htop 3.5.1: incorrect memory usage in macOS and Apple Silicon

3 participants