Skip to content

Report memory pressure and guard the background service ask - #498

Open
samoylenkodmitry wants to merge 5 commits into
mainfrom
fix/play-vitals-memory
Open

Report memory pressure and guard the background service ask#498
samoylenkodmitry wants to merge 5 commits into
mainfrom
fix/play-vitals-memory

Conversation

@samoylenkodmitry

Copy link
Copy Markdown
Owner

Why

Google Play sets memory limits from February 2027, judged as the 90th percentile of Anonymous RSS + Swap per app state. The tightest state is a foreground service with the app off screen: 1 GB on 4 GB phones, 2 GB even on 16 GB phones. The framework had no road to hand trim reports to an application, and two service-ask defects ended the process on real phones.

What

  • rememberMemoryPressure(): Android's onTrimMemory reaches applications as a collected event stream (UiHidden, Low, Critical), plus observe_memory_pressure for work that must run while the composition loop sleeps. Unit tests cover the registry and the level mapping.
  • The trim hook registers a ComponentCallbacks2 on the application context. The activity override never fired on an EMUI Android 10 phone; the registered callback fires everywhere.
  • The activity asks for the foreground service only after a first resume, and 700 ms after a pause with a resume that cancels the ask. A screen-off start used to end the process with ForegroundServiceDidNotStartInTimeException, reproduced twice on a Pixel 9 Pro on Android 16.
  • The service tracks its startForeground obligation, so a stop inside that window waits for the service to honour it first. A refused set_active logs instead of passing quietly.

Checked

  • Screen-off cold start on the Pixel 9 Pro (Android 16) and the Huawei EVR-AL00 (Android 10): process alive, no crash, no service asked.
  • Trim delivery on both the arm64 emulator (Android 15) and the Huawei: one report per event, from the system's own UI_HIDDEN too.
  • In the consuming app: the system hid the UI and the app freed its models 12 ms later, 1.36 GB anon down to 0.70 GB.
  • fmt, clippy and the service tests pass; the consuming app's 14-step robot suite passes against this branch.

cranscan consumes this branch through [patch.crates-io] until a release carries it.

🤖 Generated with Claude Code

samoylenkodmitry and others added 4 commits August 27, 2026 18:00
Android's onTrimMemory now reaches applications as a collected event
stream: rememberMemoryPressure() delivers UiHidden, Low or Critical, so
an app can free caches and warm model sessions the moment the platform
asks, instead of on its own timers.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A launch with the screen off pauses without ever resuming. Asking for a
foreground service from that state is a background start: the system
accepts the request, never creates the service, and ends the process for
the missing startForeground. Reproduced twice on a Pixel 9 Pro on
Android 16; with the guard the same start stays alive with no service
asked.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The activity's onTrimMemory override never fired on an EMUI Android 10
phone, so trim reports silently went nowhere there. A ComponentCallbacks2
registered on the application context receives every trim on every
version; onLowMemory maps to the complete level. Verified on that phone:
one "platform pressure" report per event after the change, none before.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
From the crash-fix session, verified together with this branch on the
phones: the activity asks for the foreground service 700 ms after a
pause and a resume cancels the ask, so a launch-shaped pause never
starts a service the framework then refuses. The service tracks its own
startForeground obligation, and a stop that arrives inside that window
waits for the service to honour it first. A refused set_active call now
logs instead of passing quietly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.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