WebView2 control throws OutOfMemoryException on call to EnsureCoreWebView2Async() after being loaded too many times
Describe the bug
Our WinUI 3 app allows users to navigate to various pages to define a model, compute, and review outputs. Some of these pages have WebView2 controls on them to show their content. Once we've loaded a WebView2 control 252 times, the 253rd attempt throws an OutOfMemory exception when we call EnsureCoreWebView2Async() when initializing the WebView2 control.
This was tested with the following configurations with the same behavior:
- Windows 10 (10.0.19045), WebView Runtime 125.0.2535.67 and 124.0.2478.109
- Windows 11 (10.0.22631), WebView Runtime 125.0.2535.67
Steps to reproduce the bug
- Run the attached application's
Debug|x86unpackaged configuration - Click on a
NavigationViewItemto load a page containing a singleWebView2control - Continue clicking on a
NavigationViewItemto load subsequent pages - Once you start to load the 253rd page, it will crash with an
OutOfMemoryExceptionwhen the page calls theWebView2'sEnsureCoreWebView2Async()method.
Expected behavior
We should be able to load and unload a WebView2 control as many times as necessary, as is possible with other controls like TextBoxes, RadioButtons, etc.
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000
Windows version
Windows 11 (22H2): Build 22621, Windows 10 (21H2): Build 19044
Additional context
If I don't call WebView.Close() from the Page's Unloaded event, I have been able to load the WebView2 control as many as 268 times before crashing.
I've made a similar bug report in the Microsoft Edge WebView2Feeback repository. Please let me know if that or somewhere else is a more appropriate place to report this issue. Thank you!
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!
Closed similar issues:
- WebView/WebView2 memory leak (#5622), similarity score: 0.79
- WebView2 hosted by NavigationView/Frame/Page crashes app on exit (#7632), similarity score: 0.77
- WebView2 in a Window crashes the app when the Window is closed. (#6872), similarity score: 0.75
- WebView2 lifecycle (#4752), similarity score: 0.74
- WinUI 3 app window with WebView2 crashes when closing (#8395), similarity score: 0.73
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
I've had a look at this, and it looks to me like it may be an issue with C# garbage collection. In your test app (thanks so much for that, by the way!), I added this after WebView2.Close();:
WebView.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
GC.Collect();
});
I no longer see the memory usage grow unboundedly larger with that added. Could you confirm? It's possible I'm missing something, so feel free to reopen if so.
Thanks for looking at this! After you posted, I ran through some tests before trying your solution and found that I could no longer reproduce the initial issue. I loaded the WebView2-containing page more than 600 times without a crash before giving up. So, I think the problem has already been addressed in some way.