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

WebView MAUI WinUI Memory Leak #20283

Closed
davide-cavallini opened this issue Feb 1, 2024 · 5 comments · Fixed by #23540
Closed

WebView MAUI WinUI Memory Leak #20283

davide-cavallini opened this issue Feb 1, 2024 · 5 comments · Fixed by #23540
Labels
area-controls-webview WebView fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/windows 🪟 t/bug Something isn't working t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Milestone

Comments

@davide-cavallini
Copy link

Description

Each time a "Webview" tag is added in a Page or ContentView, the memory allocation is not freed by the garbage collector.

Steps to Reproduce

No response

Link to public reproduction project repository

https://github.com/davide-cavallini/webview-winUI-maui-leak

Version with bug

8.0.6

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

The current workaround is to declare the webview in the code behind as static then changing its source binding each time the page/view is navigated.
In that way the leak happens only the first time and the memory allocated still the same.

Relevant log output

No response

@davide-cavallini davide-cavallini added the t/bug Something isn't working label Feb 1, 2024
@samhouts samhouts added platform/windows 🪟 legacy-area-perf Startup / Runtime performance labels Feb 2, 2024
@AdamEssenmacher
Copy link

Consider checking out my memory toolkit designed to address these sorts of issues: https://github.com/AdamEssenmacher/MemoryToolkit.Maui

@rmarinho rmarinho added this to the Backlog milestone Feb 2, 2024
@ghost
Copy link

ghost commented Feb 2, 2024

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@rmarinho rmarinho added the memory-leak 💦 Memory usage grows / objects live forever (sub: perf) label Feb 2, 2024
@bronteq
Copy link

bronteq commented Mar 15, 2024

Hi, I opened a duplicate issue that was closed, so I report here some information:
-the bug affects Maui 8.0.3, 8.0.6, 8.0.7, 8.0.10 (previous versions not tested)
-the same problem seems to be present in Maui Android, as described in this issue: #18021
-other reproduction project: https://github.com/bronteq/maui_webview_issue

After n page changes, Task Manager reports n WebView2 tabs related to the app, but Visual Studio memory report does not indicates the massive memory increase
image

@Eilon Eilon added t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) area-controls-webview WebView and removed legacy-area-perf Startup / Runtime performance labels May 10, 2024
@jingliancui
Copy link
Contributor

jingliancui commented Jun 10, 2024

Multiple windows each contains the webview also have this issue after window get closed, I raised another new issue #22972

@jonathanpeppers
Copy link
Member

@davide-cavallini for now, can you do something like this in your Page:

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        webView.Handler?.DisconnectHandler();
    }

(I put x:Name="webView" in the XAML)

This seems to solve the issue in your sample:

image

jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jul 10, 2024
Fixes: dotnet#20283
Fixes: dotnet#22972
Context: https://github.com/davide-cavallini/webview-winUI-maui-leak

The above sample on Windows was leaking because it does the following:

1. Your app has a single `Window`

2. Navigate to a page with a `WebView`

3. Navigate away

Because the `Window` remains alive, `WebViewHandler` subscribes
to `Window.Closed` which keeps a reference to the `WebView` and
keeps it alive indefinitely.

I was able to reproduce this in a test, that keeps the `Window`
alive before calling `AssertionExtensions.WaitForGC()`.

The solution was to move the `Window.Closed` subscription to the
`WebView2Proxy` nested class. This makes sure that the `WebView`,
its handler, etc. can be collected *before* the `Window` is closed.

I also found a secondary issue while debugging, if you call:

    webView.Close(); // MauiWebView or WebView2

If `CoreWebView2` is not initialized, it will throw a C++ exception.

We can instead do:

    if (webView.CoreWebView2 is not null)
    {
        webView.Close();
    }
@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-webview WebView fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/windows 🪟 t/bug Something isn't working t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants