Skip to content

Apply official Android memory-pressure strategies to Coil and export - #402

Draft
rosuH wants to merge 2 commits into
masterfrom
cursor/android-memory-management-f96f
Draft

rosuH wants to merge 2 commits into
masterfrom
cursor/android-memory-management-f96f

Conversation

@rosuH

@rosuH rosuH commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Reads Manage your app's memory against this photo-watermark app and applies the unused official strategies.

Already aligned (no change)

R8 minify + shrink, Coil-only UI thumbs, inSampleSize / RGB_565 thumbs, BitmapCache trim, DEBUG ProfilingManager, no Service, no largeHeap. The August Android 17 pass already covered most pillars.

Gaps this PR closes

The article’s two unused code hooks were:

  1. Release UI bitmap caches on onTrimMemory — Coil product thumbs hold 30% of available memory and were not on the trim path.
  2. getMemoryInfo() before memory-intensive workgetAvailableMemory() existed with zero callers. Full-res export is that path.

Also: preview trim ran on every trim level and always kept the focus Source, even when the process was a BACKGROUND kill candidate.

AndroidMemoryPressure now coordinates three reconstructable caches:

Signal BitmapCache Coil thumbs Preview working set
below UI_HIDDEN no-op no-op no-op
UI_HIDDEN soft ~25% soft ~25% drop Watermarked + neighbors; keep focus
BACKGROUND / onLowMemory evict evict evict all

Export calls releaseReconstructableIfNeeded first: if the system is lowMemory or remaining Java heap is under 64 MiB, those caches are dropped. The save is never skipped.

Never Bitmap.recycle() from a cache.

Deliberately not applied

Hilt (ADR-0005 Koin), LeakCanary (privacy), SparseArray split, activating the dead inBitmap pool, closing Room on BACKGROUND, converting API 29+ loadThumbnail to RGB_565.

Full audit: docs/superpowers/research/2026-08-30-manage-app-memory.md

Tests

Robolectric (22/0): AndroidMemoryPressureTest, AndroidPreviewSourceReuseTest, BitmapCacheTest, AndroidExportIntermediateReleaseTest.

Open in Web Open in Cursor 

Base automatically changed from feat/migrate_to_compose to master September 4, 2026 00:52
cursoragent and others added 2 commits September 4, 2026 08:52
onTrimMemory now coordinates BitmapCache, Coil product thumbs, and the
preview working set (UI_HIDDEN soft-trim / BACKGROUND evict). Export
releases those reconstructable caches when getMemoryInfo says the system
is low or the Java heap has less than 64 MiB remaining.

Co-authored-by: rosuH <hi@rosuh.me>
Post-14 only UI_HIDDEN and BACKGROUND remain meaningful trim levels.

Co-authored-by: rosuH <hi@rosuh.me>
@rosuH
rosuH force-pushed the cursor/android-memory-management-f96f branch from 8f3f8cd to 69f8f98 Compare September 4, 2026 00:52
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