Bitmap the memory footprint of those things calculation and loading considerations

Source: Internet
Author: User

Please support the original, thank you this article author!


In the mobile phone picture is generally refers to bitmap pictures, why to say bitmap it? Because everyone in the development of the application, will use some pictures to display the UI, the user also likes to see pictures, read the text to get information is too slow and not intuitive, if the design of good art, look at the picture is basically not how to look at your text content will know what you want to express, For example, all the shopping sites will edit a lot of product map to present to the user, this shows the image in the application of common and important. As long as the picture can not leave the topic of how to avoid the oom, because in the processing of many pictures easily oom, then the study of image processing is particularly important, let us step by step to learn the relevant knowledge of the picture.

Bitmap picture takes up memory calculation:

        bitmap images are calculated as: Width * high * pixel point number when loading into memory. You can think of the picture is composed of width row, height column matrix, each matrix element represents a pixel, each pixel is a 1byte integer times the data, the larger the data, the richer the color, the higher the image display quality. In bitmap, there is an enumeration class Config used to configure the compression format of the picture, representing how much data each pixel is stored in, the larger the value, the more color information can be stored, the richer the display, the better the result. Config.alpha_8 is 1 byte,config.rgb_565 and config.argb_4444 are 2 bytes,config.rgb_565 have no ALPHA value so more used to configure the picture without transparency, CONFIG.ARGB _8888 is 4 bytes, the general picture is according to this to configure. Here's the code to get the configuration:

static int getbytesperpixel (config config) {if (config = = config.argb_8888) {return 4;    } else if (config = = config.rgb_565) {return 2;    } else if (config = = config.argb_4444) {return 2;    } else if (config = = config.alpha_8) {return 1; } return 1;}

What you need to pay attention to when using pictures:


1, the Android system itself problems. The Android system assigns a certain amount of memory space to each application, allocates how much to see the manufacturer and the model, the value can be obtained through the Runtime class, Runtime.getruntime () Gets the instance, and then passes the MaxMemory () Method gets the maximum memory that the system can allocate for the app, TotalMemory () Gets the memory heap space currently allocated by the app, Freememory () Gets the memory currently available, expands automatically when it is exhausted, but does not exceed maxmemory. Minimum memory for different DPI allocations at different resolutions available on Google's website;

2. How big a photo is needed. Many pictures actually show on the phone do not need to completely load the original image into memory, such as my phone camera took a picture is 4208*3120, loaded into memory occupied memory is 52M, this is very scary, two photos almost put your app memory to consume light. In general, you need to load the image to be processed, this processing is mainly to reduce the size of the picture, reduce the resolution, such as your control display is the size of the 100*100, then you have to reduce the image to 100*100.

3, the timely release of memory. Before Android 2.3.3 (API level 10), bitmap pixel data and bitmap objects are stored separately, pixel data is stored in native memory, objects are stored in Dalvik heap, native The pixel data in memory is not released in a predictable manner and may cause the application to temporarily exceed its memory limit and crashes, so before Android2.3.3 (API 10) You have to call the Recycle () method to free up memory to avoid oom. The premise is to make sure the bitmap is no longer in use, otherwise the "canvas:trying to use a recycled bitmap" will appear. After Android3.0 (API 11), Bitmap's pixel data is stored with the bitmap object in the Dalvik heap, so we do not have to manually call recycle () to release the Bitmap object, and the release of the memory is given to the garbage collector.

The above is to learn some basic knowledge of bitmap and load into the memory of the phone needs attention, the next blog I will write how to better load bitmap, how to save memory, how efficient. Thank you for your visit, write bad place and have any questions please leave a message or communicate with me faster please add:coder_online public number, here not only my article, but also my small partners, a variety of technology sharing, may wish you to see, we look forward to your arrival. can also scan the following QR code, duang a bit to find us ....


Bitmap the memory footprint of those things calculation and loading considerations

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.