WebView/WebView2 memory leak
Describe the bug
WebView2 (and old WebView) exhibits a memory leak when creating and deleting the control in a loop. In my application, the user can change the content every (x) seconds and with a WebView/WebView2 control the user will get a memory leak.
Steps to reproduce the bug
Steps to reproduce the behavior: GitHub repo
- Start a loop / a timer
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; timer.Tick += Timer_Tick; timer.Start(); - In a loop logic find the old WebView/WebView2 control in a DOM
VisualTreeHelper.GetChildrenCount(parent); ..... VisualTreeHelper.GetChild(parent, i) ...) - Remove the old WebView from the DOM
(VisualTreeHelper.DisconnectChildrenRecursive(rootGrid);) - For a WebView call
webViewToRemove.Navigate(new Uri("about:blank"));For a WebView2:webViewToRemove.Close(); - Add a new WebView/WebView2 control to the DOM
rootGrid.Children.Add(newWebView); - Application crashes after 15 minutes.
Screenshots

Version Info
NuGet package version:
| Windows 10 version | Saw the problem? |
|---|---|
| Insider Build (xxxxx) | |
| May 2020 Update (19042) | Yes |
| November 2019 Update (18363) | |
| May 2019 Update (18362) | |
| October 2018 Update (17763) |
Additional context
I can reproduce, it crashes after 20 minutes.
Hi @tj-jyk, we're monitoring the issue that was opened in the WinUI repo. If the issue isn't able to be resolved with their advice we recommend that you open an Issue in the WebView2 repo
Hi @tj-jyk, thank you for opening the issue under the MicrosoftEdge organization.