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.

Visual tree element detaching and reattaching operations can lead to memory leaks

See original GitHub issue

Scenario

I need to dynamically modify ControlTheme or Style and apply the changes to the existing interface immediately.

In 11.0.0, I can only apply real-time changes to ControlTheme by using reflection to call the StyledElement.OnControlThemeChanged and StyledElement.OnTemplatedParentControlThemeChanged methods on all elements in the visual tree. Then, I call StyledElement.InvalidStyles() to apply Style changes.

I know this is not a robust and good solution. But currently, I have no other way. Then, I encountered a memory leak issue.

Describe the bug

So I modified the approach by breaking down the content of the Window from the visual tree and then putting it back to update the ControlTheme and Style of all elements under this subtree. Unfortunately, this operation also resulted in a memory leak.

I repeated this process 100 times and monitored the memory changes, and found that there were many objects in Generation 2 that increased during this process, even though I manually executed GC.Collect(), it didn’t have any effect.

The Code To Reproduce


// Note that the window should have something as it's content.

public class MainWindow : Window
{
        protected override void OnLoaded(RoutedEventArgs e)
        {
            base.OnLoaded(e);

            var content = this.Content;

            Task.Delay(3000).ContinueWith(t =>
            {
                for (var i = 0; i < 100; i++)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        this.Content = null;
                        this.Content = content;
                    });
                    Thread.Sleep(200);
                }
            });
        }
}

Expected behavior

Even without considering the rare and unconventional scenarios described above, frequent detaching and reattaching operations from the visual tree should not lead to the continuous generation of unreleased memory objects.

Screenshots

image

image

Desktop

  • OS: Windows 11
  • Version 11.0.0.

Issue Analytics

  • State:open
  • Created a month ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
liwuqingxincommented, Aug 8, 2023

No I guess I stopped debugger too early. Sorry. After having a deeper look it may be related to: #12416

There’s a PR which should address this. You can try it here: #12418 (comment)

Ok, I will see that later. Thank you 👍 @timunie

0reactions
timuniecommented, Aug 8, 2023

No I guess I stopped debugger too early. Sorry. After having a deeper look it may be related to: https://github.com/AvaloniaUI/Avalonia/issues/12416

There’s a PR which should address this. You can try it here: https://github.com/AvaloniaUI/Avalonia/pull/12418#issuecomment-1662069675

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debug DOM memory leaks with the Detached Elements tool
Use the Detached Elements tool in the Microsoft Edge DevTools to find and fix DOM memory leaks.
Read more >
memory leaks - eventContext leaking with LitElement
1 Answer. As it happens, there is a known issue with one of the web components I'm using (mwc-ripple) where it attaches event...
Read more >
Debug memory leaks with the Microsoft Edge Detached ...
A DOM memory leak occurs when an application keeps references to more and more of these detached elements, without actually reusing them later....
Read more >
Causes of Memory Leaks in JavaScript and How to Avoid ...
In this article, we will explore programming patterns that cause memory leaks in JavaScript and explain ... Event Listeners; Cache; Detached DOM Elements...
Read more >
Debug memory leaks with the Microsoft Edge ... - YouTube
We're very excited to announce the new Detached Elements tool in Microsoft Edge DevTools which will help you investigate and resolve DOM ...
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