Skip to content

[Skia] A decoded image's pixels are released only through a finalizer chain, never when the image leaves the screen #25114

Closed

Description

@mikernet

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

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

  1. mikernet commented on Oct 8, 2026

    @mikernet
    ContributorAuthor

    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):

    1. Releasing on last unsubscribe and on unload fixed navigation, but clicking quickly through large images still ratcheted the linear memory up by 100 to 200 MB per burst. The cause was a second site: a decode that finishes after its open was superseded produced a surface nothing displayed, because 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.
    2. With both parts, the wasm heap stayed flat at 661 MB across repeated worst-case loops (rapid paging through 4000x2667 slides, leaving and returning), where it previously climbed until the runtime exited at the 2 GB ceiling, repeatedly; managed memory after a full collection sits at 60 to 130 MB with only the two on-screen images alive.

    Measured with the same helper described above (forced collections, Skia instance census, reflection walk from static and thread-static roots).

  2. added 3 commits that reference this issue on Oct 8, 2026
    8620cb9
    5cdea60
    d648708
  3. added
    7.1
    on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    7.1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions