Skip to content

fix(media): free link preview, site icon and Markdown caches on memory pressure - #237

Merged
Tarquinen merged 2 commits into
devfrom
cache-memory-purge
Nov 21, 2026
Merged

Tarquinen merged 2 commits into
devfrom
cache-memory-purge

Conversation

@Tarquinen

@Tarquinen Tarquinen commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Ports iOS #309 (ab383d8a). The app's existing MemoryWarningCenter now empties these caches as well as the media reference loader:

  • Link preview and site icon loaders: memory only. Disk stays, so the next load reads it back without a request.
  • A transcript's LinkPreviewStore: decoded pictures and remembered reply links. The next draw makes them again, so nothing on screen goes blank.
  • MarkdownStreamStore and InlineCache. A part's next text parses from the start.

When it fires: onTrimMemory at RUNNING_LOW or above, and onLowMemory. On Android 14+ only UI_HIDDEN and BACKGROUND are delivered, so in practice the purge runs every time the app leaves the screen (Home, switching apps, opening a link in the browser). Android's guidance is to free UI caches there. On the emulator, switching away and back looks the same as on dev: the transcript, cards and icons are drawn in every frame of the return, with no blank, plain card or layout shift (frame by frame, 3–4 runs each on rich-link-arrival and long). The cost is a hot return about 40–50 ms slower on the long transcript, while the Markdown parses again.

Other changes:

  • Site icons keep at most 128 in memory (they had no limit), and disk hits keep the 64/128 limits too.
  • Keeping a record never evicts the one just kept, so an offline reopen still shows a stale card.
  • A transcript keeps remembered links for at most 256 replies, as on iOS.
  • Only the live singletons listen to the shared center. MemoryWarningCenter.observe returns a remover, so a closed LinkPreviewStore stops listening.

Not ported: the iOS MarkdownStreamStore eviction-order change, the xattr sweep and decode-on-draw. Follow-ups: the drawer's ProjectIconLoader cache, and the :server parsed-message cache.

Nothing visible changes, so no media.

Verified: full scripts/check.sh on the Android VM. The new tests fail with the observers, the limits, the eviction fix or the reply cap turned off. Emulator app-switch comparison against dev as above.

…y pressure

Ports iOS #309 (ab383d8a). Android already posts a memory warning
(MemoryWarningCenter, from onTrimMemory >= RUNNING_LOW and onLowMemory), but
only the media reference loader listened. Now these also empty on a warning:

- the link preview and site icon loaders' memory tier (CoalescedDiskCache);
  disk stays, so the next load reads it back without a request
- a transcript's LinkPreviewStore: decoded pictures and remembered reply
  links, which the next draw decodes and extracts again
- MarkdownStreamStore and InlineCache; the next text parses from the start

Site icons now keep at most 128 in memory (they had no cap), and disk hits
keep the 64/128 limits too (they used to write past them).
MemoryWarningCenter.observe returns a remover so a closed LinkPreviewStore
stops listening. MarkdownStreamStore's eviction-order change isn't ported.

Verified: full scripts/check.sh on the Android VM. New tests fail with the
observer and the limit turned off: diskHitsStayWithinTheMemoryLimit,
aMemoryWarningEmptiesTheLoaderButNotTheDisk, siteIconsKeepAtMost128InMemory,
aMemoryWarningEmptiesSiteIconMemory, aMemoryWarningKeepsWhatRowsShow,
inlineCacheEmptiesEveryTableOnAMemoryWarning, markdownStreamsGoOnAMemoryWarning.
- CoalescedDiskCache.remember never evicts the entry it just kept, so an
  offline reopen still shows a stale card once memory is full.
- Only the live singletons observe MemoryWarningCenter.default; test
  instances no longer pile up on the shared center.
- InlineCache registers after its tables exist.
- LinkPreviewStore keeps at most 256 remembered replies, as iOS does.
- Tests: close() stops the purge, the row-keeping test no longer reads
  the fake loader after the purge, the site-icon warning test reads the
  disk, the cold-miss test fills memory first, rememberedRepliesAreBounded.
- Parity rows and test tables updated.

Verified: `vm.sh check :feature:media :feature:transcript` passes; with
the eviction fix, the reply cap or the close() unregister reverted, the
matching tests fail.
@Tarquinen
Tarquinen merged commit e787da2 into dev Nov 21, 2026
3 of 4 checks passed
@Tarquinen
Tarquinen deleted the cache-memory-purge branch November 21, 2026 00:45
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