Android Memory Optimizer One

Source: Internet
Author: User

Common causes of memory leaks

Context Object leak

1. If a class holds a strong reference to a context object, it needs to check if its lifetime is longer than the context object. Otherwise, a context leak may occur.

2. View holds a reference to the context object in which it was created, which can cause a memory leak if the view object is passed to a strong reference that has a longer lifetime than the view context.

For example, a memory leak for view#settag (int, Object) https://code.google.com/p/android/issues/detail?id=18273

3. Assign the context object to the static variable.

Avoid context object leaks checklist

1. Check that all objects holding strong references to the context object have a life cycle beyond the context object that they hold.

2, check whether the view out of the context of the view outside the place, if there are words need to check the life cycle.

3, it is best not to have the context member variable in the tool class, try to pass in the call parameter directly when calling the function. If you must have a context member variable, you might consider using WeakReference to refer to the context object.

4. View holds a reference to the context object in which it was created, which can cause a memory leak if the view object is passed to a strong reference that has a longer lifetime than the view context.

5, check the context or view object assigned to the static variable place, see if there is a context leak.

6. Check all places where the view is placed in the container class (especially the static container class) to see if there is a memory leak. 7, the use of Weakhashmap also need to pay attention to there is no value-key reference.

7, try to use ApplicationContext.

Handler Object leaks

1, the message sent to handler is actually added to the main thread of the queue waiting for processing, each message holds its target handler strong reference.

As we usually use the anonymous inner class handler

New Handler () {    @Override    public  voidhandlemessage (Message msg) {       Mimageview.setimagebitmap (Mbitmap);    }}

The above is a simple use of handler. When using an inner class (including an anonymous class) to create a handler, the handler object implicitly holds a reference to an external class object (usually an activity) because view is attached to an activity. Handler usually comes along with a time-consuming background thread (for example, pulling pictures from the web), which notifies handler via a message mechanism after the task has finished executing (the sample is downloaded), and then handler updates the image to the interface. However, if the user shuts down the activity during a network request, and normally the activity is no longer used, it may be reclaimed during GC checking, but since the thread has not finished executing, the thread holds the Handler reference (otherwise how does it send a message to Handler?). This handler also holds the activity reference, causing the activity to not be recycled (i.e. memory leaks) until the end of the network request (the sample download is complete). Also, if you execute the handler postdelayed () method, the method will load your handler into a message and push the message into the MessageQueue, before the delay you set arrives, There will be a chain of activity, MessageQueue, message, Handler, which causes your activity to be referenced and not recycled.

Of course, for the reason that handler holds references externally, we can set the activity to a weak reference and, when unnecessary, no longer execute the internal method.

ImportAndroid.app.activity;importandroid.content.context;importandroid.os.handler;importandroid.os.message;  Importjava.lang.ref.WeakReference; Publicclass Weakrefhandlerextendshandler{WeakReference<context>Mweakcontext;  PublicWeakrefhandler (Context context) {Mweakcontext= newweakreference<context>(context); } @Override Public voidhandlemessage (Message msg) {if((Mweakcontext.get () instanceofactivity) &&( Activity) Mweakcontext.get ()). Isfinishing ( ))return ; if(mweakcontext==NULL){            return ; }        Super. Handlemessage (msg); }}

Android Memory Optimizer One

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.