Right way to dispose page with WebView in a MAUI app

ac-lap 71 Reputation points
2023-10-24T16:15:54.12+00:00

I have a MAUI Android app (sample) with 2 pages, 1 home page and 1 page containing WebView. From home page user can go to the WebView page and open a site, come back to the home page and again go to WebView page to open other site, user can do this multiple times.

After few such cycles, in edge://inspect/#devices I see multiple instances of the WebView and with page navigating away the WebView is not going away, I am concerned that this is causing memory leak.

enter image description here

I see the WebView class does not have any close/dispose/destroy kind of methods. What is the proper way to close the page containing WebView so that all resources are properly released?

I have attached the sample app which reproduces the issue. Pasting some code snippets for quick reference -

MauiAppWebView.zip

// How page with WebView is being opened
private async void OnCounterClicked(object sender, EventArgs e)
{
    List<string> siteList = new List<string>() { "https://www.google.com", "https://www.bing.com", "https://www.youtube.com", "https://www.microsoft.com" };
    Random rnd = new Random();
    await Navigation.PushAsync(new NewPage1(siteList[rnd.Next(0, siteList.Count)]));
}

// How site is being loaded in WebView
public NewPage1(string site)
{
    InitializeComponent();
    webview.Source = site;
}
// Page exit/back handling
private void GoBack(object sender, EventArgs e)
{
    Navigation.PopAsync();
}

Steps to Reproduce -

  1. Build and start the app attached in the zip on Android device.
  2. Click on 'Click me'
  3. New page with site loads, click 'Go Back'
  4. Repeat step 2 and 3 few times.
  5. Open edge://inspect/#devices
  6. Look for the device, you'll see all instances of WebView

Things I have tried so far -

  1. While existing/back the page, set webview to null.
  2. Used singleton instance of WebView in the page.

None of these things helped, and in edge inspect I still see all the previous webview pages.

Please suggest on what is the right way to dispose the page and webview.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 37,186 Reputation points Microsoft Vendor
    2023-10-25T05:51:24.45+00:00

    Hello,

    In MAUI, you can implement this requirement using the following code.

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        if (null != webview)
        {
            webview = null;
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful