"Android Memory leak" WebView article

Source: Internet
Author: User

In our blog Park app, the content of the news is displayed using WebView. In the test, we repeatedly entered, exited a news 10 times, observed

Objects has been growing, repeatedly triggering GC, but has not been recycled, occupy more and more high memory, so alert to this webview may leak memory

As follows:

StackOverflow on the Android WebView memory leak (poor quality of domestic search results, novice friends recommend to this site search answers)

There's an answer that probably means:

The webview tag we define in XML, the activity is held by this webview as the context parameter , so when the activity ends up wanting to release itself, but still being quoted by WebView, So the GC won't recycle, causing a memory leak

The way to solve this is

1: Use container wrap WebView

< framelayout     Android:id = "@+id/web_container"     android:layout_width= "Fill_parent"    android:layout_height= "Wrap_ Content "/>

2: Manually create the WebView, using ApplicationContext as the parameter. In OnDestroy, call the removeallviews of the container and call WebView's destroy

 Public classTestactivityextendsActivity {PrivateFramelayout Mwebcontainer; PrivateWebView Mwebview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.your_layout); Mwebcontainer=(framelayout) Findviewbyid (R.id.web_container); Mwebview=NewWebView (Getapplicationcontext ());    Mwebcontainer.addview (Mwebview); } @Overrideprotected voidOnDestroy () {Super. OnDestroy ();        Mwebcontainer.removeallviews ();    Mwebview.destroy (); }}

After the modification, repeated testing, found that the GC can normally recover WebView resources, very good

Finally give some suggestions on how Android avoids memory leaks, the original text is as follows

  • Keep long-lived references to a context-activity (a reference to an activity should has the same life cycle as the Activity itself)
  • Try using the context-application instead of a context-activity
  • Avoid non-static Inner Classes in an activity if you don't control their life cycle, use a static inner class and make a W Eak reference to the activity inside. The solution to this issue are to use a static inner class with a weakreference to the outer class, as do in Viewroot and Its W inner class for instance

The general meaning is that memory cannot be recycled, and in many cases it is because the activity is being referenced by an element that prevents the GC from being recycled.

So

1: Keep the life cycle of the activity-holding object consistent with the activity. (in particular, the various asynchronous operation Lifecycles will be longer than the activity)

2: Use global object application instead of activity

3: Note that the inner class of the activity holds an implicit reference to the activity (otherwise you can access the variables of the outer class). ), the static modifier inner class is used to eliminate implicit references to external classes and to access the properties of external classes with WeakReference. An example

"Android Memory leak" WebView article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.