Android efficient memory saves your image memory,

Source: Internet
Author: User

Android efficient memory saves your image memory,
Android efficient memory saves your image memory

During memory optimization, we found that in addition to solving the memory leakage problem, we only had to find a way to reduce the actual memory usage. In the App, most of the memory may be occupied by our images, so reducing the image memory usage can bring a direct effect.

I. Memory occupied by an image

Assume that one image is 600*800 pixels, and the disk space occupied by the image is assumed to be kb.

What is the relationship between the image memory size and the disk space used?

The size of disk space is not the size of memory occupied by images. The disk space is the size required to store images on disks. The memory size is the size occupied by the memory loaded into the memory. The two are just the same unit, and they are not a concept in essence.

How much memory does an image occupy?

The formula for calculating the memory usage of an image is as follows: The image height * The Image Width * the memory size occupied by one pixel. In Android, the memory occupied by one pixel is generally 4 bytes by default, therefore, the memory occupied by the above image is: 800*600*4 byte = 1875KB = 1.83 MB. Why is it 4 bytes? Must it be 4 bytes? These two questions will be discussed in detail later.

How does the image directory affect the memory?

In Android, the storage directory of images and the screen density of mobile phones affect the actual size of images finally loaded into the memory. For example, assume that our images are placed in the xhdpi directory, the memory occupied by the images in this article is as follows.

  • Devices with a screen density of 2: 800*600*4 byte = 1.83 M
  • Devices with a screen density of 3: 800*1.5*600*1.5*4 byte = 1.83*2.25 M = 4.12 M
  • The screen density here refers to the density variable in the android. util. DisplayMetrics class, which is a float value. This article will not describe more about the screen density.

  

Therefore, when calculating the memory usage of images, consider the Directory and screen density of the images. These two factors actually affect the image height and width, android will pull and compress images.

2. Save your image memory by 2.1 and minimize your image

The image memory usage is calculated as follows: Image Height * Image Width * memory size occupied by one pixel, so if the image height and width change to 2 times the original width and height, then the memory will be 4 times the original. Therefore, the usage principles of images can be summarized as follows:

For example, to achieve a linear gradient, you can use the followingDrawableImplementation:

 

2.2 compress images in memory

When loading a large image, you must compress the image and use the proportional compression method to process the image directly in the memory.

Options options = new BitmapFactory. options (); options. inSampleSize = 5; // 1/5 of the source image. If it is set to 2, it is 1/2 Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. id. myimage, options );

In this case, the image quality will deteriorate. The larger the value of inSampleSize, the worse the image quality.

2.3 when reading the size and type of bitmap, the image is not loaded into the memory.

Sometimes we get an image, maybe only to get some information about the image, such as the width and height of the image, and do not need to be displayed on the interface, at this time, we can not load images into the memory.

BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.id.myimage, options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;String imageType = options.outMimeType;
2.4 recycling after use

Because the outer layer of Android uses java, the underlying layer uses the memory space allocated by the C language for image objects. Therefore, although our external architecture seems to have been released, the internal layer is not necessarily completely released. We recommend that you release the memory space of the internal layer after using the image.

If (! BitmapObject. isRecyled () {// Bitmap object is not recycled bitmapObject. recycle (); // release System. gc (); // remind the System to recycle it in time}
2.5 reduce the color quality of the image to be displayed 2.5.1 Color Model

RGB (ARGB)

The RGB color mode is a color standard in the industry) the changes of the three color channels and their overlapping to obtain a variety of colors, RGB represents the colors of the red, green, and blue channels, this standard covers almost all colors that human vision can perceive and is one of the most widely used color systems. There is also a color model with transparency Alpha in Android, that is, ARGB.

 

2.5.2 digital encoding of RGB color values in computers

Without considering transparency, there are three methods to express the color value of a pixel in the computer:

In Java, float variables occupy 32 bits, int variables occupy 32 bits, and short and char variables both occupy 16 bits, the color of a pixel is encoded in floating-point notation. The memory usage is 96 bytes, that is, 12 bytes. The 24-bit integer notation is used for encoding as long as an int type variable, it occupies 4 bytes (8 characters in height and 24 characters in color). It is encoded in 16-bit integer notation. Only one short type variable occupies 2 bytes; therefore, we can see that using integer notation to encode the color value can greatly save the memory, of course, the color quality will be relatively low. In Android, Bitmap is also encoded in integer format.

2.5.3 RGB encoding format (integer encoding) in Android)
  • RGB888 (int): Each of the R, G, and B Components occupies eight places.
  • RGB565 (short): The R, G, and B Components occupy 5, 6, and 5 places respectively.
  • RGB555 (short): RGB components are expressed in 5 bits (the remaining 1 bits are not used)
  • ARGB8888 (int): each of the components A, R, G, and B occupies 8 places.
  • ARGB4444 (short): each of the components A, R, G, and B occupies four places

In the Bitmap. Config class of Android, there are constants such as ARGB_8888, ARGB_4444, and RGB565. Now we can know what they represent.

 

In Android, the default encoding format is ARGB_8888. Therefore, when calculating the image memory size at the beginning of the article, the memory size occupied by each pixel is 4 bytes, for example, if you use ARGB_8888 encoding to load a 1920*1200 image, it will probably occupy 1920*1200*4/1024/1024 = 8.79MB of memory.

2.5.4 reduce the color quality of the image to be displayed

Uses a low memory usage encoding method. For example, Bitmap. Config. ARGB_4444 saves more memory than Bitmap. Config. ARGB_8888, such as 1920*1200 images.

  • ARGB_8888: 1920*1200*4/1024/1024 = 8.79 MB
  • ARGB_4444, RGB565: 1920*1200*2/1024/1024 = 4.39 MB
Iii. Summary

In Android, you must pay attention to image usage. In most cases, memory usage is high, and OOM occurs because image resources are improperly used. Do not add a large image to the Android project blindly. You can use. 9, and the. 9 image itself is as small as possible. In addition, you can use the rendering implementation without adding an image resource. In some cases, you can reduce the color quality of images without affecting the user experience. For example, if you do not need transparency, you will not need transparency.

 

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.