0. notice-earlier version
- To use WebView without causing a memory leak, the first thing to do is to not define the WebView node in XML, but to generate it dynamically when needed. That is, you can place a linearlayout-like ViewGroup node where you use WebView, and then dynamically generate it when you want to use WebView:
webview Mwebview = new webview ( Getapplicationgcontext ());
linearlayout MLL Span class= "pun" >= Findviewbyid ( r id xxx
MLL addview mwebview
Then be sure to call explicitly in the OnDestroy () method:
protected void onDestroy() {
super.onDestroy();
mWebView.removeAllViews();
mWebView.destroy()
}
Note: New WebView (Getapplicationgcontext ()), must be passed in ApplicationContext if the context of the activity is passed in, the reference to the memory will always be maintained. Some people use this method to solve the problem of keeping references when the activity is eliminated. But you will find that if you need to open the link in webview or you open the page with Flash, get your webview want to pop up a dialog, Will cause a forced type conversion error from ApplicationContext to Activitycontext, causing your app to crash.
1. What leads to Memory leak1.1 innerclass
public class InnerClassActivity extends Activity{
private static Leak mLeak;
class Leak {
int a = 3;
private Context mLeakContext;
Leak(Context context) {
mLeakContext = context;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mLeak = new Leak(this);
Toast.makeText(this, "This is InnerClassActivity", Toast.LENGTH_SHORT).show();
}
}
1.2 Singleton
public class Singleton {
private static Singleton instance;
private Context mContext1;
private Singleton(Context context) {
this.mContext1 = context;
}
public static Singleton getInstance(Context context) {
if(instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton(context);
}
}
}
return instance;
}
}
1.3 Webview-earlier Version
public class WebViewCreateActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_create);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
linearlayout layoutparams Layoutparams = new linearlayout layoutparams viewgroup layoutparams match_parent viewgroup layoutparams match_parent
WebView webView = new WebView(this);
webView.setLayoutParams(layoutParams);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl("https://www.baidu.com/");
ll.addView(webView);
Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
}
}
2. How to detect the Memory Leak2.1 Android Studio
- GC manually
- Dump Java Heap
2.2 Memoryleak in our Project
Brand |
firmware |
Leak Point |
Samsung |
4.0.4 |
Lightappmanager |
Millet |
5.0.1 |
Lightappmanager/maccessibilitymanager |
Huawei |
6.0 |
Lightappmanager/maccessibilitymanager |
2.2.1 Lightappmanager Leakage
3 times after the manual GC, the memory grows:
Leak Point 1:lightappmanager leakage
Leakage Point 2:webview-wrapper getsystemservice leakage
3. Official attitude
It‘s 2016 now and, as far as I can see it, the issue still hasn‘t been resolved. I tested it on Nexus 5 and Nexus 6 with the latest WebView updates (since the component is now separate from the OS itself). Could someone, please, take a look at this issue?!
Source: https://code.google.com/p/android/issues/detail?id=9375
webview-A memory leak exists