Repository navigation
fix(wasm): Release image decoder memory after each decode (backport #24848) (backport #24911) - #25781
Merged
agneszitte merged 4 commits intoOct 28, 2026
Conversation
2 of 5 tasks
agneszitte
approved these changes
Oct 28, 2026
This was referenced Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.tsruns one dedicated worker pernavigator.hardwareConcurrency(8 on an iPad), each with oneOffscreenCanvas. A decode resized the canvas to the image and never shrank it, and never closed theImageBitmap, which was freed only when the worker's own GC ran. On the C# side, theJSObjectreturned byLoadFromArraywas 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.ImageLoader.ts): aftergetImageData, the bitmap is closed and the canvas is shrunk back to 0×0 in afinally, so errors release them too. Width and height now come from theImageData, because a closed bitmap reports 0×0.ImageSourceHelpers: theJSObjectis disposed withusing.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.CopyPixelscopies 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.
Chrome, full SamplesApp: a probe test decodes 8 images concurrently per round, 3 rounds of 2048².
JSObjectaccounts for about 30 MiB.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_ImageandGiven_BitmapSourcepass: 44 passed, 0 failed. Three tests are skipped by[Ignore], andWhen_Transitive_Asset_With_Link_Loadedis inconclusive in the browser, as it is on master.New test:
Given_BitmapSource.When_Browser_Workers_Are_Reusedcalls the worker API directly. ThroughImage, 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 whileWhen_Alpha_Blending,When_Exif_Rotated_From_StreamandWhen_Exif_Rotated_MsAppxstill pass.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.BrowserandUno.UI.RuntimeTests.Skiacompile fornet10.0with analyzers, code-style enforcement and warnings as errors.net11.0was not built locally.Not validated:
Backports
ImageLoader.tsis identical onservicing/6.8andrelease/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 anSKImagewithout copying it. When backporting, drop that commit and keep only theusinginImageSourceHelpers.cs. That file is the one conflict a dry-run merge predicts on both branches.Not in this PR
errorormessageerrorhandler and no timeout. A worker that crashes leaves its decode waiting forever and drops out of the pool. This already happens on master.hardwareConcurrencyworkers at startup, with no cap and no idle shutdown. Several decodes at once still peak at roughly workers × (bitmap + canvas + pixels).PR Checklist ✅
Screenshots Compare Test Runresults.🤖 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).