Android5.1 system WebView Memory leak scenario
Last Update:2016-05-12
Source: Internet
Author: User
<span id="Label3"></p>Problem phenomenon<p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">(this article, from the private blog of Route 0th, I in the development process of browsing framework, in this way, to circumvent the problem of memory Leaks.) )</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">In the Android5.1 system, you will find that there is WebView leakage of the app, but also more serious. And just happen on Android 5.1 system.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">The GC roots is as Follows:</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"></p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">Every new opening of this webviewactivity will happen once the WebView instance cannot be freed and a new object is Added.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">The two Appsearchwebview instances in the sample are caused by two Open.</p></p>Problem analysis<p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">It is still relatively simple to analyze this problem, and according to this referential relationship, we can see intuitively that it is due to the mcomponentcallbacks of Appsearch (extends Application) has been strongly referencing awcomponentcallbacks, resulting in an inability to release. Then awcomponentcallbacks-awcontents > Appsearchwebview.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">The key to finding out by analyzing code is that awcontents here is why Awcomponentscallbacks is not released.</p></p><pre class="highlight" style="margin-top:0px; margin-bottom:15px; padding:8px 12px; font-size:15px; border:1px solid rgb(232,232,232); overflow-x:scroll; background:rgb(255,255,255)"><code style="border:0px; padding:1px 0px; background-color:rgb(238,238,255)"><code style="border:0px; padding:1px 0px; background-color:rgb (238,238,255)"> @Override public void Onattachedtowindow () {if (isdestroyed ()) return; If (misattachedtowindow) {LOG.W (TAG, "onattachedtowindow called when already attached. Ignoring "); Return } ... mcomponentcallbacks = new Awcomponentcallbacks (); Mcontext.registercomponentcallbacks (mcomponentcallbacks); } @Override public void Ondetachedfromwindow () {if (isdestroyed ()) return; If (!misattachedtowindow) {LOG.W (TAG, "ondetachedfromwindow called when already detached. Ignoring "); Return } ... if (mcomponentcallbacks! = Null) {mcontext.unregistercomponentcallbacks (mcomponentcallbacks); Mcomponentcallbacks = null; }...... } </code></code></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">See this code can not see what problem, Onattach when Register,detach time unregister, there is no problem.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">But why?</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">Is it due to the return of the If (isdestroyed ()) return?</p></p> <blockquote style="margin:0px 0px 15px; padding:0px 0px 0px 15px; color:rgb(130,130,130); border-left-width:4px; border-left-style:solid; border-left-color:rgb(232,232,232); font-size:18px; letter-spacing:-1px; font-style:italic; font-family:Helvetica,Arial,sans-serif; background-color:rgb(253,253,253)"> <blockquote style="margin:0px 0px 15px; padding:0px 0px 0px 15px; color:rgb(130,130,130); border-left-width:4px; border-left-style:solid; border-left-color:rgb(232,232,232); font-size:18px; letter-spacing:-1px; font-style:italic; font-family:Helvetica,Arial,sans-serif; background-color:rgb(253,253,253)"> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px">This judgment returns True when Webview.destroy () is Called.</p> </blockquote> </blockquote><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">Let's see where to call the Webview.destroy ()</p></p><pre class="highlight" style="margin-top:0px; margin-bottom:15px; padding:8px 12px; font-size:15px; border:1px solid rgb(232,232,232); overflow-x:scroll; background:rgb(255,255,255)"><pre class="highlight" style="margin-top:0px; margin-bottom:15px; padding:8px 12px; font-size:15px; border:1px solid rgb(232,232,232); overflow-x:scroll; background:rgb(255,255,255)"><code style="border:0px; padding:1px 0px; background-color:rgb(238,238,255)">// source from WebViewActivity@Overrideprotected void onDestroy() { super.onDestroy(); if (mWebView != null) { mWebView.destroy(); }}</code></pre></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">Many applications should do this, including the system browser, when the activity destroy, call Webview's Destroy. and has been working very well.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">Through debugging, It is true that this call is the Cause. OnDestroy occurs before Ondetach.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">So why is there no problem with the code before Android 5.1?</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">Look at the Code:</p></p><pre class="highlight" style="margin-top:0px; margin-bottom:15px; padding:8px 12px; font-size:15px; border:1px solid rgb(232,232,232); overflow-x:scroll; background:rgb(255,255,255)"><pre class="highlight" style="margin-top:0px; margin-bottom:15px; padding:8px 12px; font-size:15px; border:1px Solid RGB (232,232,232); overflow-x:scroll; Background:rgb (255,255,255) "><code style=" border:0px; padding:1px 0px; background-color:rgb (238,238,255) ">// Awcontents.java @Override public void Ondetachedfromwindow () {if (!misattachedtowindow) {log.w (T AG, "ondetachedfromwindow called when already detached. Ignoring "); Return } ... if (mcomponentcallbacks! = Null) {mcontext.unregistercomponentcallbacks (mcomponentcallbacks); Mcomponentcallbacks = null; }......}</code></pre></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)">That's less than 5.1 code.<code class="highlighter-rouge" style="font-size:15px; border:1px solid rgb(232,232,232); padding:1px 5px; background-color:rgb(238,238,255)">if (isDestroyed()) return;</code></p></p>Circumvention methods <ul style="margin:0px 0px 15px 30px; padding:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"> <ul style="margin:0px 0px 15px 30px; padding:0px; color:rgb(17,17,17); font-family:Helvetica,Arial,sans-serif; font-size:16px; line-height:24px; background-color:rgb(253,253,253)"> <li><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px">The solution can: Remove the webview from the parent before destroy, and you can also detach in Advance.</p><pre class="highlight" style="margin-top:0px; margin-bottom:15px; padding:8px 12px; font-size:15px; border:1px solid rgb(232,232,232); overflow-x:scroll; background:rgb(255,255,255)"><code style="border:0px; padding:1px 0px; background-color:rgb(238,238,255)"> protected void onDestroy() { if (mWebView != null) { ((ViewGroup) mWebView.getParent()).removeView(mWebView); mWebView.destroy(); mWebView = null; } super.onDestroy(); }</code></pre></li> </ul> </ul><p><p>Android5.1 system WebView Memory leak scenario</p></p></span>