Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebView2 memory leak #1633

Closed
tj-jyk opened this issue Aug 10, 2021 · 14 comments
Closed

WebView2 memory leak #1633

tj-jyk opened this issue Aug 10, 2021 · 14 comments
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@tj-jyk
Copy link

tj-jyk commented Aug 10, 2021

Description

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. I opened already 2 Issues in WindowsAppSDK repo and in WinUI repo

Version
SDK: 1.0.902.49
Runtime: 92.0.902.62
Framework: UWP
OS: Win10 10.0.19042 Build 19042

Repro Steps
UWP Project GitHub repo

  1. Start a loop / a timer var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) }; timer.Tick += Timer_Tick; timer.Start();
  2. In a loop logic find the old WebView/WebView2 control in a DOM VisualTreeHelper.GetChildrenCount(parent); ..... VisualTreeHelper.GetChild(parent, i) ...)
  3. Remove the old WebView from the DOM (VisualTreeHelper.DisconnectChildrenRecursive(rootGrid);)
  4. For a WebView call webViewToRemove.Navigate(new Uri("about:blank")); For a WebView2: webViewToRemove.Close();
  5. Add a new WebView/WebView2 control to the DOM rootGrid.Children.Add(newWebView);
  6. Application crashes after 15 minutes.

Screenshots
image

Additional context

AB#35385513

@tj-jyk tj-jyk added the bug Something isn't working label Aug 10, 2021
@champnic champnic added the tracked We are tracking this work internally. label Aug 17, 2021
@champnic
Copy link
Member

Thanks for the bug report @tj-jyk! I've added it to our backlog to take a look.

In the meantime do you need to remove and create a new WebView2 every time? Can you reuse the WebView2 when you need to change content?

@tj-jyk
Copy link
Author

tj-jyk commented Aug 18, 2021

Hi @champnic.
Yes, it's possible and its work. But if the component has a memory leak, will be better to get an update, otherwise you will have to spend a lot of time rewriting the existing code. I found many articles in the internet and nobody has a right answer.

@champnic
Copy link
Member

I completely agree we should try and fix this memory leak - I just wanted to make sure that your app wasn't blocked by this issue :)

@AdamCaviness
Copy link

In my case it appears a leak stems from the HwndSourceKeyboardInputSite. I am calling Dispose() but the WebView2 instance remains in memory.

image

@champnic
Copy link
Member

Hey @AdamCaviness - Can you share how you are hooking up to the WebView2 and how/when you are calling Dispose?

@AdamCaviness
Copy link

@champnic Gladly. In my scenario, the WebView2 component is not placed in a dedicated window but is instead shown directly in the MainWindow with an active screen approach. The sample (WpfWebView2Leak) demonstrates the same single window app approach. The WebView2 component is disposed in the WebScreen Close button handler. The derived WebView2 approach is intended to address a Windows sleep issue with WebView2. I used dotMemory to profile the sample. Thanks.

@champnic
Copy link
Member

Thanks for sharing. I've verified that the WebView2 processes and most of the WebView2 code is being cleaned up at least, as those are where most of the memory usage would be. I wonder if there's a bug in WPF around making sure the HwndSourceKeyboardInputSite gets removed from the list. We'll take a closer look. Thanks!

@MatevzRopret
Copy link

MatevzRopret commented Jan 12, 2022

I'd just like to report that we have found the same issue: attaching the Webview to the UI is what causes the leak.
If the webview is not attached to the UI it won't leak.

Here is an example project (VS solution)

Webview2MemoryLeakTest.zip

EDIT:
This memory leak happens also if you create just eg 10 webviews and pool them and add/remove the cyclicaly.
Though it seems that the leak is less severe in this case, but it's still there.

@fabioparodi
Copy link

I have the same problem.
I've modified a very old and stable C++/MFC application to use WebVIew2. Then scripted to create-show-hide-dispose the WebVIew2 window in a cycle every 10 seconds. I can see the proces private bytes increase, it is very regular.
I am going to pool and reuse the WebView2 windows - never ever dispose.
Still an open bug

@fabioparodi
Copy link

image

@juipinjao
Copy link

fanioparoid
I face the same problem on a MFC app. too. Just wonder if you find any solution? Thanks

@fabioparodi
Copy link

fabioparodi commented Oct 6, 2022 via email

@agenne
Copy link

agenne commented Oct 20, 2022

The original issue posted by @tj-jyk (my colleague) seems to be solved.
Updated the WebView2 version to WinUI 2.8.1.
It is still necessary to cleanup, but it is sufficient to call Close on the webview which is much better than navigate to about:blank and call Gc.Collect

@champnic
Copy link
Member

Glad to hear it's working now @agenne!

For others on this thread - if you are still seeing the issue and it's not in WinUI 2.8.1 or WindowsAppSDK, please open a separate issue for us to track. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

7 participants