Skip to content

fix(wasm): Release image decoder memory after each decode (backport #24848) (backport #24911) - #25781

Merged
agneszitte merged 4 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-24911
Oct 28, 2026
Merged

agneszitte merged 4 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-24911

Conversation

@mergify

@mergify mergify Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: related to unoplatform/kahua-private#547

PR Type:

🐞 Bugfix

What changed? 🚀

On Skia WebAssembly, the browser image decoder keeps every worker's last image in memory for the rest of the session. ImageLoader.ts runs one dedicated worker per navigator.hardwareConcurrency (8 on an iPad), each with one OffscreenCanvas. A decode resized the canvas to the image and never shrank it, and never closed the ImageBitmap, which was freed only when the worker's own GC ran. On the C# side, the JSObject returned by LoadFromArray was never disposed, so the response, including the w×h×4 pixel buffer transferred from the worker, stayed alive until the wrapper's finalizer ran. The managed GC isn't prompted by that memory, so it can be a long wait.

This reduces the browser process footprint, which is what iOS memory limits count. It doesn't change the growth of WebAssembly linear memory (HEAPU8.length): the heap peak was the same in every measured build.

  • Worker (ImageLoader.ts): after getImageData, the bitmap is closed and the canvas is shrunk back to 0×0 in a finally, so errors release them too. Width and height now come from the ImageData, because a closed bitmap reports 0×0.
  • ImageSourceHelpers: the JSObject is disposed with using.
  • ImageSourceHelpers, master only: the RGBA→BGRA swap now happens in place on the managed copy, instead of allocating a second full-size array per decode. CopyPixels copies the pixels into native memory either way.

There are no public API changes. The response returned to C# ({error, bytes, width, height}) is unchanged.

Validation

Memory was sampled from outside the browser with macOS footprint, at fixed phases of each run.

  • Safari 26.6 (desktop), worker only: a script-only page runs each build's worker code without .NET: 8 workers, 3 rounds of 2048² PNGs.

    • Canvas memory: 128 MiB, never released, before the fix; 0 after.
    • Page process 30 s after the last decode: 333 MiB before, 94–110 MiB after (baseline 123 MiB).
    • Decoded bitmaps at their peak: +333 MiB before, +124 MiB after.
  • Chrome, full SamplesApp: a probe test decodes 8 images concurrently per round, 3 rounds of 2048².

    • Page process 5 s after the last decode: 1,286–1,414 MiB before, 1,012–1,013 MiB after.
    • Browser-side allocations (PartitionAlloc): 333–350 MiB before, 61–63 MiB after (baseline 59 MiB). With only the worker change, 92 MiB, so disposing the JSObject accounts for about 30 MiB.
    • Full GCs for the same decodes: 53–58 before, 42–46 after, from the in-place swap.
  • Decode time (Chrome, worker only, same-size decodes on one worker): 256² took 1.5–2.4 ms per decode before and 1.5–1.6 ms after; 1024² about 21 ms in both. The Safari timings varied up to 6× between runs of the same build and are not reported.

  • Runtime tests (Skia WebAssembly, headless Chrome, Release): all of Given_Image and Given_BitmapSource pass: 44 passed, 0 failed. Three tests are skipped by [Ignore], and When_Transitive_Asset_With_Link_Loaded is inconclusive in the browser, as it is on master.

  • New test: Given_BitmapSource.When_Browser_Workers_Are_Reused calls the worker API directly. Through Image, a broken worker can't be seen: a failed browser decode falls back to SKCodec and the image still opens. With a worker that throws instead of returning pixels, this test fails while When_Alpha_Blending, When_Exif_Rotated_From_Stream and When_Exif_Rotated_MsAppx still pass.

    • It decodes one more image than there are workers, so at least one worker decodes again after its reset.
    • It then decodes invalid bytes followed by a valid image.
    • It checks dimensions, byte count and the premultiplied first pixel.

    The test passes without the fix too. It guards the reset and error paths; the memory effect is shown by the measurements above. The expected pixels were also checked on Safari 26.6, which returns the same values as Chrome.

  • Build: Uno.UI, Uno.UI.Runtime.Skia.WebAssembly.Browser and Uno.UI.RuntimeTests.Skia compile for net10.0 with analyzers, code-style enforcement and warnings as errors. net11.0 was not built locally.

  • Not validated:

    • On an iPad.
    • The SamplesApp runtime tests in Safari: a covered Safari window throttles timers, which stalled the runs.

Backports

ImageLoader.ts is identical on servicing/6.8 and release/stable/6.7, and the worker change and the test apply there unchanged. The in-place swap commit is master only: 6.x wraps the pinned managed array in an SKImage without copying it. When backporting, drop that commit and keep only the using in ImageSourceHelpers.cs. That file is the one conflict a dry-run merge predicts on both branches.

Not in this PR

  • The worker pool has no error or messageerror handler and no timeout. A worker that crashes leaves its decode waiting forever and drops out of the pool. This already happens on master.
  • The pool creates hardwareConcurrency workers at startup, with no cap and no idle shutdown. Several decodes at once still peak at roughly workers × (bitmap + canvas + pixels).
  • On master, the R/B swap could move into the worker's premultiply loop, and the managed copy could be skipped by copying the JS buffer straight into native memory.

PR Checklist ✅

🤖 Generated with Claude Code


This is an automatic backport of pull request #24848 done by [Mergify](https://mergify.com).
This is an automatic backport of pull request #24911 done by [Mergify](https://mergify.com).

(cherry picked from commit 2e1ed42)
(cherry picked from commit 951a8aa)
(cherry picked from commit 979d6f2)
(cherry picked from commit de9aab5)
(cherry picked from commit 2db70df)
(cherry picked from commit 2ca885c)
(cherry picked from commit 5006b9e)
(cherry picked from commit 114b9dd)
@agneszitte
agneszitte merged commit 56ad5cc into release/stable/6.7 Oct 28, 2026
13 of 18 checks passed
@agneszitte
agneszitte deleted the mergify/bp/release/stable/6.7/pr-24911 branch October 28, 2026 02:28
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