How to fix Android memory problems

Source: Internet
Author: User

Android project due to the loading of more pictures, so often reported memory overflow errors, when the project contains a large number of pictures, or pictures too large, may be oom, common Hu solution is as follows:

Method 1: Zoom out of the picture
                Bitmapfactory.options Options = new Bitmapfactory.options ();                Options.insamplesize = 4;


Method 2: Take a soft reference to the picture and do the Recyle () operation in a timely manner

              Softreference<bitmap> Bitmap;              Bitmap = new softreference<bitmap> (pbitmap);         if (bitmap! = null) {              <span style= "White-space:pre" ></span>   if (bitmap.get ()! = NULL &&! Bitmap.get (). isRecycled ()) {                   bitmap.get (). Recycle ();                   bitmap = null;             }             }

Method 3: Design and encode the complex listview properly:

1. Note the use of Convertview and holder mechanisms in the reuse of adapter

Public View GetView (int position, View Convertview, ViewGroup parent) {  if (Convertview = = null) {            v = minflater.i Nflate (Resource, parent, false);            Final int[] to = MTo;            Final int count = to.length;            Final view[] holder = new View[count];            for (int i = 0; i < count; i++) {                Holder[i] = V.findviewbyid (To[i]);            }    V.settag (holder);        } else {        }    }


2. The above method attempt has not been successful and can be used with the lazy loading data


Method 4: Single page, toggle N-Time after OOM on screen

1. See if there are any large pictures in the page layout, such as the background map. Remove the relevant settings from the XML and set the background map in the program (in the OnCreate () method):

<span style= "White-space:pre" ></span>drawable bg = getresources (). getdrawable (R.drawable.bg);        Xxx.setbackgrounddrawable (RLADDETAILONE_BG);
Note When Activity destory, bg.setcallback (null); Prevent the activity from being released in a timely manner 

2. Similar to the above method, directly load the XML configuration file into a View and put it into a container, and then call This.setcontentview (view view) directly;

Avoid repeated loading of XML


Method 5: Reuse as few code as possible during page switching, such as repeating the database call, reusing certain objects, etc...


Method 6:android Heap Memory can also define its own size and optimize heap memory allocations for Dalvik virtual machines

Note If you use this method: project build target can only select the <= 2.2 version, otherwise the compilation will not pass. So it's not recommended in this way

     Private final static int cwj_heap_size= 6*1024*1024; <span style= "White-space:pre" ></span>private final static float target_heap_utilization = 0.75f;        Vmruntime.getruntime (). Setminimumheapsize (cwj_heap_size);        Vmruntime.getruntime (). Settargetheaputilization (target_heap_utilization);






How to fix Android memory problems

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.