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
How to Launch Termux Through Another Android App
Create a Ninepatch/Ninepatchdrawable in Runtime
Java.Lang.Illegalargumentexception: Contains a Path Separator
Phonegap - Add a Splash Screen for Android App
Can't Create Handler Inside Thread Which Has Not Called Looper.Prepare()
Google Speech Recognition Timeout
Android Custom Dropdown/Popup Menu
Listview with Onitemclicklistener
Changing the Android Overflow Menu Icon Programmatically
Android 2.1 Programmatically Unmount Sdcard
Do I Need All Three Constructors for an Android Custom View
How to Make a Gridlayout Fit Screen Size
How to Pass Integer from One Activity to Another
The APK File Does Not Exist on Disk
Android Support Repo 46.0.0 with Android Studio 2.3
Getview Returning Null When Fragment Has Been Created from an Activity