Acquisition and testing of memory size occupied by a picture in Android

Source: Internet
Author: User
Tags config garbage collection log

When you load more pictures in an Android program, you may get out of memory and cause the program to crash. This is partly because the Android system itself has limited memory size for each individual process (there are 16m,64m,128m,256m and so on), on the other hand, because the Android system is slow to garbage collection of picture resources (article http:// jiangnane.com/index.php/archives/230 in the Android source analysis, found that the Android Setimageviewbitmap (Bitmap Bm) method of the source code does not establish a new Bitmap, Instead, it uses BM in a citation way, which causes BM to be referenced in multiple places.

How much memory does that picture occupy? This blog post mainly records some of the data and test results that I looked up when I tested the bytes of a picture.

1. Method of representation of pictures

The Android Bitmap.config gives the bitmap a pixel-by-store approach, with a rgb_565,argb_8888,argb_4444,alpha_8 of four species. rgb_565 means that the red, green and blue colors are stored with 5,6,5 bits, and a pixel occupies a 5+6+5=16 bit. rgb_8888 means that red, green, blue, and translucent are stored separately with 8,8,8,8 bits, and one pixel takes up 8+8+8+8=32 bits. In this case, if the picture is read in rgb_8888, then the amount of memory consumed will be twice times the rgb_565 read mode.

Usually we load pictures for Imagview either by setdrawable or by using ANDROID:SRC in the XML file, As you can see from the BitmapFactory.Options.inPreferredConfig documentation, the default way to load a picture is to read it in rgb_8888.

2. Get the number of bytes occupied by bitmap

More than 12 of the API can be obtained directly, and the following is obtained by multiplying the bytes of the high and the rows.

protected int sizeOf (Bitmap data) {   
    if (Build.VERSION.SDK_INT < build.version_codes. HONEYCOMB_MR1) {return data.getrowbytes ()   
        * Data.getheight ();   
    } else {return   
        data.getbytecount ();   
    }   
}

Source: http://stackoverflow.com/a/2408164/1767800

3. Read the picture in rgb_565 way

Public  Bitmap Readbitmap (context context, int resid) {    
    bitmapfactory.options opt = new bitmapfactory.options ();    
    opt.inpreferredconfig = Bitmap.Config.RGB_565;     
   Opt.inpurgeable = true;    
   Opt.ininputshareable = true;    
      Get resource picture    
   InputStream is = Context.getresources (). Openrawresource (RESID);   
   Return Bitmapfactory.decodestream (is,null,opt);   
}

Reference: http://blog.csdn.net/yangyangiud/article/details/12835885

4. Get the size of ImageView and drawable

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

To get the ImageView and the drawable size to be obtained in onwindowfocuschanged, the result returned in OnCreate is 0.

public void Onwindowfocuschanged (Boolean hasfocus) {   
        ImageView imageview= (ImageView) Findviewbyid (r.id.test1);   
        LOG.V ("TestResult", "Width=" +imageview.getwidth () + "height="
        +imageview.getheight ());   
        LOG.V ("TestResult", "Drawawidth=" +imageview.getdrawable (). getbounds (). Width () +   
                "drawableheight="
        + Imageview.getdrawable (). getbounds (). height ());   
    

Reference: http://stackoverflow.com/a/15128508/1767800

Test code in 5.Oncreate ()

protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   
               
        Setcontentview (R.layout.activity_main);   
        ImageView imageview= (ImageView) Findviewbyid (R.ID.TEST1);   
        drawable drawable = imageview.getdrawable (); int bitmapwidth = Drawable.getintrinsicwidth (); This is the bitmap ' s width int bitmapheight = Drawable.getintrinsicheight (); This is the bitmap ' s height log.v ("TestResult", "bitmapwidth=" +bitmapwidth+ "bitmapheight="   
               
        +bitmapheight);   
        Bitmap bitmap= ((bitmapdrawable) imageview.getdrawable ()). Getbitmap (); if (Build.VERSION.SDK_INT < build.version_codes.   
        HONEYCOMB_MR1) {log.v ("TestResult", "Bitmap bytes are" +bitmap.getrowbytes () *bitmap.getheight ());   
        else {log.v ("TestResult", "Bitmap bytes are" +bitmap.getbytecount ()); } BITMAP=READBITMap (this, r.drawable.pic_1000_562); Above API 12 You can directly get the number of bytes occupied by bitmap if (Build.VERSION.SDK_INT < build.version_codes.   
        HONEYCOMB_MR1) {log.v ("TestResult", "Bitmap bytes are" +bitmap.getrowbytes () *bitmap.getheight ());   
        else {log.v ("TestResult", "Bitmap bytes are" +bitmap.getbytecount ()); }   
               
       
    }

6. Test results

The original picture size is 1000*562, the screen resolution of the mobile phone is 480*800. As can be seen from the diagram, the Android:src loading method takes up twice times the number of bytes used in the rgb_565 loading mode. and 1124000=2*1000*562 bytes. And while the bitmap is displayed when it is displayed on the phone, the length of the drawable is consistent with the size of the picture.

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.