question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak when subscribing to observables from XAML in a data template

See original GitHub issue

Bug 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. image

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:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
grokyscommented, Jun 11, 2021

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.

0reactions
unofficialdevcommented, Jul 12, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Massive memory leak when binding ItemsControl to an ...
All better! The problem is with binding my observable collection to the ItemsSource property of my ItemsControl.
Read more >
Binding to observable memory leak · Issue #3527
This simple app demonstrate a memory leak using the observable binding operator ^. The app adds and removes view models to an observable ......
Read more >
[BUG] MemoryLeak on WPF · Issue #3091 · reactiveui ...
There is something keeping ViewModels and Views from ReactiveUI in memory causing MemoryLeak. Solution provided in repository might seems ...
Read more >
Can memory leaks come from switchMap or pipe? : r/angular
Yes. If the switchMap operator contains an observable that is a persistent watcher and does not unsubscribe itself it will cause a memory...
Read more >
3 Patterns to Prevent Memory Leaks in Angular ...
Learn how three patterns to prevent memory leaks in Angular Components. ... observable data sources, one property for each subscription and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found