Some ideas about memory overflow (Android)

Source: Internet
Author: User

Analysis of the creation process of the Android application window (Activity) Runtime context

Some ideas about memory overflow (Android)

http://m.oschina.net/blog/54936

1. Can be used in the context of application. Because if you use the context of activity, if you use too much activity, if some resources are still referencing the resources of the activity's context, it will cause the activity to be not recycled, which may lead to Oom,

2. Bitmap recovery. You can unbind in Fragment, but do not recycle (). If you want to recycle to make sure that this bitmap is no longer used, such as Fragment or Activity left and right sliding, bitmap do not recycle. But can be untied.

3. After Dialog is exhausted, after dismiss, set to null.

4. Picture do not do memory cache, can do disk cache. For example, download images for volley. Do disk caching

SOURCE Analysis:

    protected voidOnDestroy () {if(debug_lifecycle) SLOG.V (TAG, "OnDestroy" + This); Mcalled=true; //dismiss any dialogs we is managing. if  (mmanageddialogs! = null   final  int  numdialogs = Mmanageddialogs.size ();  for  (int  i = 0; i < numdialogs; i+ +  Mmanageddialogs.valueat (i);                     if   (Md.mDialog.isShowing ()) {                Md.mDialog.dismiss (); }} mmanageddialogs  = null  ; }        //Close any cursors we is managing.        synchronized(mmanagedcursors) {intNumcursors =mmanagedcursors.size ();  for(inti = 0; i < numcursors; i++) {managedcursor C=Mmanagedcursors.get (i); if(c! =NULL) {c.mcursor.close ();        }} mmanagedcursors.clear (); }        //Close any open Search dialog        if(Msearchmanager! =NULL) {msearchmanager.stopsearch (); } getapplication (). dispatchactivitydestroyed ( This); }

The following actions are performed before OnDestroy

    @Override    protectedvoid  OnDestroy () {          // stop thread         Commonutils.stopasynctask (mhasmsgtask);          Commonutils.stopasynctask (mgetextradatatask);        Stattimer.cancel ();        Extradatatimer.cancel ();        Hassystemmsgtimer.cancel ();        Haslettertimer.cancel ();         Super . OnDestroy ();    }

Picture unbind:

Com.xingjiabi.shengsheng.base.XjbBaseActivity

    protected Static void unbinddrawable (activity activity) {        null;         Try {            = ( (viewgroup) Activity.findviewbyid (Android. r.id.content))                    . Getchildat (0);         Catch (Exception e) {            e.printstacktrace ();        }         if NULL ) {            pictureutil.unbinddrawables (Rootview);        }    }

    /**     * Final will be destroyed when the reference is executed. Will the Activity be destroyed when it is destroyed?     * Static will not be destroyed until the end of application * *     

Method 1:

The previously requested network image optimization method is:

In the ListView, the postion is passed into the method of requesting the picture, and after the request succeeds, the view is obtained according to the postion, and then the value is set. This way the thread does not occupy view, resulting in the context not being released.

Method 2:

Now open source, for example volley is to put Imgview reference in.

About Handler's static

Handler write static has a benefit is not dependent on the activity of the Context, so even if Handler quilt thread reference, it will not lead to the end of the activity, because the child thread is still referencing Handler, resulting in Acti Vity's Context is not actually recycled.

Bad code common in Android (Java) that causes memory leaks:

1. Querying the database without closing cursors

2. When constructing adapter, no cached Convertview is used

3. Bitmap object Not in use call Recycle () not released in time

4. No timely release of object references

For example: for example, a context or other custom object passed between the two activity must be released immediately after use: activity = NULL; Context = null; Object = NULL, and if possible, notify the system to recycle after releasing the object: System.GC (); it's best!

Some ideas about memory overflow (Android)

Related 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.