Skip to content

Every CompositionObject is finalizable and its finalizer queues no-op work on the UI thread #25042

Description

@MartinZikmund

Current behavior 🐛

Every CompositionObject has a finalizer (~CompositionObject() { Dispose(); } in src/Uno.UI.Composition/Composition/CompositionObject.cs). Running off the UI thread, DisposeInternal then always enqueues StopAllAnimations onto the UI dispatcher, even when the object has no animations (_animations is null), which is nearly always:

private protected virtual void DisposeInternal()
{
    if (Dispatching.DispatcherQueue.Main.HasThreadAccess) { StopAllAnimations(); }
    else { Dispatching.DispatcherQueue.Main.TryEnqueue(StopAllAnimations); } // always taken from the finalizer thread
}

Consequences for every collected element (each owns one or more visuals):

  • Every visual is finalizable: it survives an extra GC, is promoted, and goes through the finalization queue.
  • Each one allocates a delegate plus a dispatcher queue item and wakes the UI thread for a no-op.
  • The dead visual and its subgraph (VisualCollection, List<Visual>, handlers) stay alive until the UI thread drains the queue. While the UI thread is busy, this grows without bound.

Measured (Skia desktop, Release): creating and dropping 100k Border { Child = new TextBlock() } while the UI thread is busy retains 253-257 MB of dead visuals (a heap dump shows 321k BorderVisual/TextVisual held by 1.28M queued DispatcherQueueHandlers, all StopAllAnimations).

Expected behavior 🎯

Unreachable composition objects with no animations are collected without UI-thread involvement and without finalization. A visual with a running animation is still reachable through the animation (AnimationFrame subscription, compositor), so the finalizer has nothing to do in practice.

How to reproduce it (as minimally and precisely as possible) 🔬

  1. In a Skia desktop app, on the UI thread, create and drop 100k new Border { Child = new TextBlock() } without yielding.
  2. GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); from a worker thread while the UI thread stays blocked.
  3. Observe the retained managed heap (hundreds of MB) and the dispatcher queue length.

Workaround 🛠️

None.

Renderer 🎨

  • Skia

Affected platforms 📱💻🖥️

All Skia targets (shared composition code). Present in 6.7 and 7.0.

Uno.Sdk version (and other relevant versions) 📦

6.7.135 and 7.0 master.

IDE version 🧑‍💻

N/A

Anything else we need to know? 💬

Same pattern as the DependencyObject finalizer removed in #23703, which made element creation ~5x faster. Related: #24184 (ContainerVisual weak GCHandle).

Activity

  1. self-assigned this
    on Oct 6, 2026
  2. added
    kind/bug
    triage/untriaged
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/performancekind/bugtriage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions