Android Menory Summary

Source: Internet
Author: User

    1. It is not recommended to use static variables in the Activity , consider using application. Of course,static final exception

But application also do not cache an Activity using the View, if the cache must also manually empty the application in the Activity Destroy () View 's cache.

    1. The thread caused a memory leak.

public class MyActivity extends Activity {

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

New MyThread (). Start ();

}

Private class MyThread extends thread{

@Override

public void Run () {

Super.run ();

Do somthing

}

}

}

Assuming that the MyThread run function is a time-consuming operation, when we turn on the thread, the device's horizontal screen is turned into a vertical screen, which is typically recreated when the screen is converted Activity, according to our thinking, the old activity should be destroyed, but in fact it is not.

Since our thread is the inner class of activity, a reference to activity is saved in MyThread , when MyThread The run function does not end,MyThread is not destroyed, so it references the old The Activity will not be destroyed, so there is a memory leak problem

3. Bitmap and drawable

Comparison of Bitmap and drawable

Compare items

Bitmap

Drawable

Display sharpness

Same

Same

Memory consumption

Big

Small

Support Scaling

Is

Is

Support hue chromatic aberration adjustment

Is

Whether

Support rotation

Is

Is

Supports transparent colors

Is

Is

Drawing speed

Slow

Fast

Support Pixel operation

Is

Whether

Drawable is better than Bitmap in the two very critical points of memory footprint and drawing speed (theGoogle Android Development example mentions drawing The Bitmap is faster than the drawable, but after the actual test, it turns out to be the opposite.
Bitmap with respect to the drawable can also adjust color chromatic hue and directly to the image of each phase of the operation.

    1. Bitmap Bitmap occupies memory

Like a picture of a 320*480,

320*480*config+1k(image Info)

android.graphics.bitmap.config= argb_8888 (32bit)

argb_4444 (16bit)

rgb_565 (16bit)

/**

* Read pictures of local resources in the most memory-saving way

* @param context

* @param resId

* @return

*/

public static Bitmap Readbitmap (context context, int resId) {

Bitmapfactory.options opt = new bitmapfactory.options ();

Read by 2 bytes per pixel , default argb_8888 is 4 bytes

Opt.inpreferredconfig = Bitmap.Config.RGB_565;

Not enough memory to automatically recycle this bitmap when reading pictures

Opt.inpurgeable = true;

should be a shared stream so that after the memory is not reclaimed, the call can be automatically read in the background

Opt.ininputshareable = true;

get a picture of a resource

InputStream is = Context.getresources (). Openrawresource (ResId);

Return Bitmapfactory.decodestream (is,null,opt);

}

5.

Exception capture to avoid program crashes

try{

Bitmap Bmp=bitmapfactory.decodefile (path,options);

}catch (OutOfMemoryError e) {

prompt the system for memory recovery

System.GC ();

Clear Program Cache

ClearCache ();

}

    1. Vmruntime.getruntime (). Setminimumheapsize (NewSize);

HeapHEAP) isVMSThe most memory-intensive portion of the, usually dynamically allocated. The size of the heap is not immutable, and there is usually an allocation mechanism to control its size. Like the initialHEAPis a4Mlarge, when4Mthe space is occupied by more than75%, reassign the heap to8Mbig; when8Mbe occupied over75%, the allocation heap is16Mlarge. Upside down, when16MInsufficient heap utilization30%, reduce its size to8Mlarge. The size of the heap, especially compression, usually involves a copy of the memory, so the size of the change heap has an adverse effect on efficiency.

The above is just an example, but you can see three parameters:max heap Size, min heap size, heap utilization(stack utilization).

Max heap Size, which is the upper value of heap memory, The default value for Android is 16M(some models are 24M), This cannot be changed for normal applications. function setminimumheapsize actually just changes the lower limit of the heap, it can prevent too frequent heap memory allocation, when setting the minimum heap memory size exceeds the upper limit value, still take the upper limit of the heap, for memory shortage does not work.

Settargetheaputilization (float newtarget) can set the percentage of memory utilization, and when the actual utilization deviates from this percentage, the virtual opportunity in the GC , adjust the heap memory size so that the actual occupancy rate is closer to a percentage.

Rivate final static floattarget_heap_utilization = 0.75f; in the program onCreate , you can call vmruntime.getruntime (). Settargetheaputilization (target_heap_utilization); can be.

6. the object scope of an inner class is outside The scope of Activity: for example, an internal class is defined to store the data, and the object of the inner class is passed to other activity or Service and so on. Because an object in an inner class holds a reference to the current class, it also holds a reference to the Context . The workaround is to extract the inner class into a separate class, or to avoid The scope of the inner object scope beyond the activity

Android Menory Summary

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.