Repository navigation
Description
Current behavior
On Skia heads, when an Image stops showing a BitmapImage (the element is discarded, the page is navigated away from, or Source changes), nothing releases the decoded pixels. The ImageSource keeps its _imageData after the last subscriber leaves, the CompositionImageSurface it holds is only disposed by its finalizer, and the Skia image behind it is only destroyed when its own wrapper is finalized. The pixel memory therefore survives the element by several garbage collections, and on WebAssembly that is what drives the process to the 2 GB limit even with the bitmap cache disabled.
On WebAssembly the cost is worse than the pixels themselves. In the shipped build (Uno.UI.dll 6.8.0-dev.212, decompiled), ImageSourceHelpers.ReadFromStreamAsCompositionSurface takes the browser-decoded pixels as a managed byte[], pins it with a GCHandle and wraps it with SKImage.FromPixels(info, pointer, releaseProc); the handle is freed by the release callback when the native image is destroyed. So a 4000x2667 image is a 40 MB pinned array on the managed heap whose lifetime is: BitmapImage becomes garbage, collection 1 queues the surface finalizer, finalizer disposes the SKImage wrapper, native unref runs the release callback, the handle is freed, collection 2 frees the array. Master has since moved to SKImage.FromPixelCopy (SkiaImageDecoderBackend.CreateImage), which removes the pinned array but keeps the same lifetime for the native copy, and the collector cannot see native memory at all.
Measured in a document viewer with 4000x2667 slides, cache disabled (EnableBitmapImageCache = false), paging through slides and then calling a helper that does GC.Collect(2, Forced, blocking, compacting), WaitForPendingFinalizers, GC.Collect again and reports GC.GetTotalMemory(false):
| Full collection | Managed after it |
|---|---|
| 1 | 454 MB |
| 2 | 539 MB |
| 3 | 451 MB |
| 4 (after paging) | 788 MB |
| 5 (after paging) | 912 MB |
| 6, a minute later, no activity | 620 MB |
| 7, a minute later, no activity | 579 MB |
Only one 40 MB Skia image is alive in SkiaSharp's instance table at every one of those points (the slide on screen); the hundreds of megabytes that drain over the later collections are pixel arrays whose wrappers are gone but whose handles have not been released yet. Each page through six slides adds about 245 MB to the wasm linear memory (sampled every 15 s with the route), and that memory never comes back because wasm memory only grows: a later 40 MB allocation reuses a hole only if a contiguous one exists, and pinned arrays that are still waiting on their finalizers cannot be compacted around, so roughly every other burst extends the heap instead of reusing it.
Expected behavior
A decoded image nothing displays should not stay in memory until the GC gets to its finalizers. WinUI keeps the surfaces of an image that left the tree for about a second, in case it comes back, and then frees them: CImageSource::LeaveImpl registers the surface wrapper for cleanup, and CCoreServices::TrimMemoryOnSurfaceWrappers (xcpcore.cpp), at the end of every render walk, frees those that have been off the tree for sc_imageSurfaceWrapperReleaseDelayMilliseconds, 1 s (corep.h explains that freeing them at once on leave caused regressions, because reloading an image takes several frames). An image displayed again after that is decoded again from the encoded data WinUI's ImageCache keeps.
On Uno the same lifetime would be: when an ImageSource loses its last subscriber, the decoded data it owns waits about 1 s and is then released, unless something subscribes again first, and the next Subscribe decodes it again. WinUI's wait has no size bound, but WinUI decodes an image at its rendered size by default (a 4000x2667 slide shown 1000 pixels wide holds under 3 MB), while Uno decodes it in full unless DecodePixelWidth is set (about 43 MB, plus the texture drawn from it). So the waiting images should also be bounded in bytes, releasing the oldest first once they exceed the bound, so that paging through large images cannot pile a second of decodes into memory, which WebAssembly's 2 GB linear memory, which never shrinks, cannot afford.
Workarounds tried
FeatureConfiguration.Image.EnableBitmapImageCache = false(and [Skia] BitmapCreateOptions.IgnoreImageCache still adds the load to the static bitmap cache #25098 / fix(imaging): Don't cache IgnoreImageCache loads #25100 forIgnoreImageCache): necessary, but it only removes the cache's own retention; the per-view lifetime above remains.DecodePixelWidthon full-size images routes the decode throughSKCodecat the target size with no managed copies, which bounds the cost, but it is a cap on image quality rather than a fix and still leaves release to finalizers.
Environment
- Uno.WinUI 6.8.0-dev.212, Uno.Sdk 6.8.0-dev.60, Uno.Wasm.Bootstrap 10.1.0-dev.219
- .NET 10.0.401, runtime pack 10.0.12
- Heads: net10.0-browserwasm (Skia renderer, Chromium) where the limit is hit; the lifetime is the same on net10.0-desktop, where the address space hides it.
Related: #25098, #25099 (the retention paths found alongside this one).
Activity
mikernet commentedon Oct 8, 2026
Fix in #25124. Two things learned while validating it in the reporting app (WebAssembly, bitmap cache off, 6.8.0-dev.212 with the fix ported):
Open's dispatched continuation dropped a result that arrived after cancellation and a requester cancelled out of waiting on its uncached load never saw the result. The PR releases at both sites; the new test holds the UI thread until the decode has produced its surface, then changes the source before the continuation can apply it.Measured with the same helper described above (forced collections, Skia instance census, reflection walk from static and thread-static roots).
fix(imaging): Release a decoded image when nothing displays it
fix(imaging): Release a decoded image when nothing displays it
fix(imaging): Per-result image ownership, unload release and stream c…