Memory leak when subscribing to observables from XAML in a data template
See original GitHub issueBug description
Subscribing to an observable from XAML (using a caret ^ syntax, e.g. IsVisible="{Binding Foo^}") in a data template causes a memory leak: the binding recipient remains in the memory after it’s long gone from the visual tree.
Likely related to #3527
Reproduction
Bind to a long-lived observable in a data template.
Minimal example, based on the default template: repo link (most relevant stuff is in the MainWindow). Dummy controls are used to track a leak.
To confirm a leak, I’ve used the managed memory profiler in VS 2019.

Expected behavior
All bindings made in XAML should be auto-disposed when control is detached. That seems to be true when using normal bindings (e.g. IsVisible="{Binding $parent[UserControl].Foo}" — included in the example).
Desktop
- Windows 10 Pro (v2004)
- Avalonia 0.10.3
Commentary
In my 100%-reactive app this issue causes colossal memory leaks: just switching a page bumps the memory gauge by 50-70 Mb. It stacks nicely with (up to) 350 Mb from the graphics bug (the one that causes memory issues when resizing a window).
I do not know if the issues is caused by a data template or a binding, but having the bug down the tree causes a huge chunk of the tree to leak, likely through a parent reference.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (4 by maintainers)

Top Related StackOverflow Question
Yeah this is a difficult one. I’ve been unable to write a unit test so far that reproduces the problem, even. Seems to be related to the fact that an observable is producing a collection via the stream operator which creates data templates which themselves have observables subscribed via the stream operator.
I need to move onto something else now, but will hopefully have time to take another look soon.
I’m having a similar problem. i try to rewrite 1 wpf app with avalonia, wpf app only use ~50mb memory, but avalonia app go up to ~400mb