A brief discussion on image memory consumption in Android app

Source: Internet
Author: User

It should be clear that memory is a valuable resource for children's shoes that have been developed in mobile applications. If we can make good use of the limited memory, it will be very helpful to improve the performance of the application. In the actual application development of image memory for the entire application of a very large proportion, we only understand how the image is loaded into memory in order to better optimize the memory of the image.

So let's explore the use of images in Android apps.

picture occupies memory size = high picture in memory * image width in memory * Number of bytes per pixel

1, is the physical height and width of the picture loaded into memory aspect the same?

The loading of pictures in the app is generally divided into two cases, the size of which is loaded into memory is not the same:

First, the picture resource is placed in drawable or mipmap, and is read in r.drawable.* or r.mipmap.* way. Most of these are applied transduction files, etc.;

Second, the picture resource is not obtained through the R file. For example, stored in SD file or network medium;

For the first case, is by the system through the current device density to select read for the density directory of the picture resources, the resource matching is to be retrieved by the corresponding density directory, if the corresponding directory is not more than the current density small directory search, only to find the image so far.

Property name ldpi mdpi hdpi xhdpi xxhdpi xxxhdpi
Table of contents corresponds to DPI 120 160 240 320 480 640

The size of this type of loading picture in memory is calculated as follows:

in-Memory length = (picture true length/image directory dpi) * Device dpi

For example: The true height of the picture is 192px, the picture resource is placed in the XHDPI directory, the test device dpi=560 so loaded into memory height = (192/320) * 480 =288

In the second case, the address of the image resource is specified directly by the developer, and the image is not processed by the system:

in-memory length = picture true length

2, How is the number of bytes in pixels calculated?

This question is clearly explained in the official website and is determined by Bitmap.config.

The default bitmap in the Android system. Config is Bitmap.Config.ARGB_8888 so each pixel accounts for 4 bytes

Let's verify that the above is true: the same picture size is used in hdpi,xhdpi,xxhdpi,xxxhdpi already sdcard 192px*192px.

The device tested is: 1080*1920 density=3.0 dpi=480

As you can see, the size of the memory corresponding to the different directory pictures

sdcard: aspect = 192px memory =192*192*4 = 144kb

hdpi: Aspect = (192/240) *480=384px 384*384*4=576kb

xhdpi: Aspect = (192/320) *480=288px 288*288*4=324kb

xxhdpi: Aspect = (192/480) *480 = 192px 192*192*4=144kb

xxxhdpi: Aspect = (192/640) *480 = 144px 144*144*4=81kb

In order to verify that the memory in the actual operation is in accordance with our calculation formula, we can use the monitoring function of as to dump out the runtime dump file for analysis and verification.

You can verify that the memory size of the picture matches the previous formula: 192*192*4=147456b

Finally, there are some suggestions for using pictures in your app:

1, the graph resources can not be randomly placed folder, it is best to follow the corresponding transduction to the corresponding file home, avoid because access to other files and resulting in the image of the memory increase.

2, for only a set of graph resources, as far as possible to choose high-precision size cut. Refer to the device information of the current mainstream equipment to select the corresponding accuracy as a standard for the cut chart. At present, I use xxhdpi as the standard of cut chart.

3, corresponding to the non-identical R file reference picture, you can choose to set the Bitmap.config property control picture memory size, you can choose Bitmap.Config.RGB_565 way, compared to the default Bitmap.Config.ARGB_ 8888 will reduce the memory footprint by half

A brief discussion on image memory consumption in Android app

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.