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

[Problem/Bug]: Memory leak? WebView2 and parent window not released after close #4286

Closed
cuiliang opened this issue Jan 10, 2024 · 6 comments
Assignees
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@cuiliang
Copy link

cuiliang commented Jan 10, 2024

What happened?

Using the smaple application: https://github.com/MicrosoftEdge/WebView2Samples/tree/main/SampleApps/WebView2WpfBrowser

  1. start the application
  2. New window mutliple times
    image
  3. Close the windows, and leave one to keep application running.
  4. Use dotmemory, attach to the process, and take snapshots.
  5. WebView2 and MainWindow objects never got GC collected.

image

image

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

120.0.2210.121

SDK Version

1.0.2210.55

Framework

WPF

Operating System

Windows 11

OS Version

No response

Repro steps

See issue description.

Repros in Edge Browser

No

Regression

No, this never worked

Last working version (if regression)

No response

AB#48526819

@cuiliang cuiliang added the bug Something isn't working label Jan 10, 2024
@cuiliang cuiliang changed the title [Problem/Bug]: Memory leak? WebView2 and parent window not released after closed [Problem/Bug]: Memory leak? WebView2 and parent window not released after close Jan 10, 2024
@cuiliang
Copy link
Author

This is another test, after window closed more than 10 minutes and Force GC in dotMemory, objects still not GC collected.
image

@vbryh-msft vbryh-msft added the tracked We are tracking this work internally. label Jan 17, 2024
@vbryh-msft
Copy link
Contributor

@cuiliang thank you for reporting the issue. I have created work item to take time to look into it.

@cuiliang
Copy link
Author

@vbryh-msft Besides, if the HostObject used in AddHostObjectToScript() hode a reference of host window, there will be another
referenct path live after window closed.

@fabiorocha fabiorocha assigned monica-ch and unassigned vbryh-msft Jan 22, 2024
@monica-ch
Copy link
Contributor

@cuiliang We've fixed the issue in Edge canary 126.0.2536.0+. Please test and let us if WV2 objects are still leaking.

Also, there is a small issue in WebView2WpfBrowser where the code isn't calling webView.Dispose() upon Close WebView. We'll soon push that sample app fix to our public repo but please call WV2 dispose for testing.

@TWhidden
Copy link

Can we assume this has not been fixed as of yet? Have the same issue on 128.0.2739.42.

The instance count continues to grow every time our WPF control creates a WebView2. If we hook into Unload, and call Dispose(), it does release everything, however we can't depend on that because Unload would be called even if it's in a TabControl and you change tabs. In our application, new controls are created and removed regularly, so these are stacking up.

@champnic
Copy link
Member

champnic commented Sep 5, 2024

Closing this issue as fixed, as it was specific to our sample app. @TWhidden The key is you need to find the right place in your app where you no longer need a specific WebView2 control and ensure Dispose() is getting called. Or re-use existing controls. If you are still running into an issue, feel free to ask a question in our Discussions, or open a bug if you believe there is an issue on the WebView2 side.

@champnic champnic closed this as completed Sep 5, 2024
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

5 participants