# # 0. Notice-earlier version
* To use WebView without causing a memory leak, the first thing to do is not to 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:
"' Javawebview mwebview = new WebView (getapplicationgcontext ()); LinearLayout MLL = Findviewbyid (r.id.xxx); Mll.addview (mwebview);
Then be sure to call explicitly in the OnDestroy () method:
"' javaprotected 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 leak
# # 1.1 Innerclass
"' Javapublic 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
"' Javapublic 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
"' Javapublic 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 Leak
# # # 2.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 leak
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 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 are 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