Memory Leak in Webview

Memory leak in WebView

I conclude from above comments and further tests, that the problem is a bug in the SDK: when creating a WebView via XML layout, the activity is passed as the context for the WebView, not the application context. When finishing the activity, the WebView still keeps references to the activity, therefore the activity doesn't get removed from the memory.
I filed a bug report for that , see the link in the comment above.

webView = new WebView(getApplicationContext());

Note that this workaround only works for certain use cases, i.e. if you just need to display html in a webview, without any href-links nor links to dialogs, etc. See the comments below.

Android Fragment Webview Memory Leak

Change

//If I comment this line out, there is no memory leak
mWebView = new WebView(this.getActivity().getApplicationContext());

&

@Override
public void onDestroy()
{
super.onDestroy();
if (mWebView != null)
{
mWebView.loadUrl("about:blank");
mWebView.destroy();
mWebView = null;
}
}

To

mWebView = new WebView(getActivity()); 

&

@Override
public void onDestroy()
{
// null out before the super call
if (mWebView != null)
{
mWebView.loadUrl("about:blank");
mWebView = null;
}
super.onDestroy();
}

UWP App WebView Leaks Memory, doesn't clear images


UWP App WebView Leaks Memory, doesn't clear images

WebView is complex element. And it has own garbage collection rules, For keep render performance, it will cache a lot of data that cause memory keeps growing and gc process is slow. we can't have it both ways.

For my experience, you could set the WebView Source as "about:blank" repeatedly that could release most data immediately.

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
int count = 0;
var timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
timer.Start();
timer.Tick += (s, p) =>
{
TestWebView.Source = new Uri("about:blank");
count++;
if (count == 20)
{
timer.Stop();
}
};
}


Related Topics



Leave a reply



Submit