Android efficient memory makes your pictures save memory
In memory optimization, we found that in addition to resolving memory leaks, the only thing left was to reduce the actual memory footprint. In the app, most of the memory may be occupied by our pictures, so reduce the memory footprint of the image can bring a direct effect.
How much memory does one or one images occupy
Let's assume that we have a picture that is 600 * 800 pixels, and the image disk occupies a space size assumption is 100KB.
What does the image memory size have to do with disk footprint?
The size of the disk footprint is not the size of the picture that occupies memory, and the disk footprint is the amount of space that is required to store the picture on disk, and the memory size is the amount of memory that is loaded into memory. Two units are just the same, the essence is not a concept.
How much memory does a picture occupy?
The image occupies the memory of the formula is: Picture height * Picture width * A pixel occupied memory size, in Android in general, the default one pixel memory is 4 bytes, so the above image occupies memory is: * * 4 byte = 1875KB = 1.83M. Why is it 4 bytes? Must be 4 bytes? These two questions are carefully followed.
What is the memory effect of the directory in which the picture resides?
In Android, the image's storage directory and the screen density of the phone affect the actual size of the image loaded into the memory, for example: assuming that our images are placed in the XHDPI directory, the memory size of our images in this article is as follows.
- Device with a screen density of 2: * * 4byte = 1.83M
- Device with a screen density of 3: * 1.5 * * * 1.5 * 4byte = 1.83 * 2.25M = 4.12M
- The screen density described here refers to the density variable in the Android.util.DisplayMetrics class, which is a float value, and more on screen density is not introduced.
So, to calculate the memory size of the picture, to consider the picture of the directory and screen density, these two factors actually affect the image of the high-width, Android will be pulled up and compression of the picture.
Two, let your picture save memory 2.1 to minimize your picture
The memory footprint of a picture is: Picture Height * Image width * A pixel of memory size, so the height of the picture if all the original width of twice times, then the memory will become 4 times times the original. So the principle of use of the picture can be summarized as follows:
- Use as small a diagram as possible
- Use the. 9 figure,. 9 figure itself to be as small as possible
- Draw yourself (overwrite the view's OnDraw) or use drawable to draw
For example, to achieve a linear gradient effect can use the following drawable implementation:
2.2 Compressing pictures in memory
When loading a large picture, you need to compress the picture, and use the proportional compression method to process the image directly in memory.
Options options = new Bitmapfactory.options (); options.insamplesize = 5; The original one-fifth, set to 2 is One-second bitmap bitmap = Bitmapfactory.decoderesource (Getresources (), r.id.myimage, Options);
In doing so, it is important to note that the image quality will become worse, the higher the value of the Insamplesize setting, the worse the picture quality.
2.3 Reading bitmap dimensions and types without loading the picture into memory
Sometimes we get a picture, maybe just to get some information about this picture, than the width of the piece, height and other information, do not need to display the interface, this time we can not load the picture into memory.
Newtrue; Bitmapfactory.decoderesource (Getresources (), r.id.myimage, options); int imageheight = options.outheight; int imagewidth == options.outmimetype;
2.4 After use, recycle
Since the Android outer layer uses Java, the underlying uses the memory space assigned by the C language to the picture object. So our exterior, although it seems to be released, but the inner layer is not necessarily completely released, we use the picture after the best release of the inner memory space.
if (!bitmapobject.isrecyled ()) { // Bitmap Object Not recycled bitmapobject.recycle (); // Release System.GC (); // Alert System for timely recovery }
2.5 Reduce the picture color quality to display 2.5.1 color model
RGB (ARGB)
RGB color mode is a color standard of industry, is through the red (R), Green (G), Blue (B) three color channels and their superposition to get a variety of colors, RGB is the red, green, blue three channels of color, This standard almost includes all the colors that human vision can perceive, and is one of the most widely used color systems. In Android There is also a color model that contains the transparency alpha, which is ARGB.
2.5.2 RGB encoding of color values in computer
In the case of transparency, the color value of a pixel is represented in the computer in the following 3 ways:
- floating-point encoding: For example, float: (1.0, 0.5, 0.75), each color component accounts for 1 float fields, where 1.0 indicates that the component's value is full red or full green or full blue.
- - bit integer encoding: For example 24-bit: (255, 128, 196), each color component 8 bits, the value range 0-255, where 255 means that the value of the component is full red or full green or full blue.
- - bits of integer encoding: such as 16-bit: (31, 45, 31), the 1th and 3rd color components accounted for 5 bits, the value range of 0-31, the 2nd color component accounted for 6 bits, the value range of 0-63.
In Java, a variable of type float is 32 bits, a variable of type int is 32 bits, and a variable of type short and char is 16 bits, so it can be seen that the color of a pixel is encoded by floating-point notation, the memory consumption is 96 bits or 12 bytes, and the 24-bit integer notation is used to encode the As long as an int type variable, occupies 4 bytes (high 8 bits empty, low 24 bits for color), and 16-bit integer notation, as long as a short type variable, accounting for 2 bytes, so you can see the integer representation of the color value, can greatly save memory, of course, The color quality will also be relatively low. An integer encoding is generally used when acquiring bitmap in Android.
2.5.3 android in RGB encoding format (integer encoding)
- RGB888 (int): R, G, b components each accounted for 8 bits
- RGB565 (short): R, G, B components accounted for 5, 6, 5 bits respectively
- RGB555 (short): RGB components are represented by 5 bits (the remaining 1 bits are not used)
- ARGB8888 (int): A, R, G, b components each accounted for 8 bits
- ARGB4444 (short): A, R, G, b components each accounted for 4 bits
In Android's Bitmap.config class, there are constants such as argb_8888, argb_4444, and RGB565, and now you can see what they mean.
In the Android system by default the encoding format is argb_8888, so at the beginning of the article to calculate the size of the image memory when the memory size of each pixel is 4byte, such as the use of argb_8888 encoded in a 1920*1200 picture, will probably occupy 1920* 1200*4/1024/1024=8.79MB of memory.
2.5.4 reduce the color quality of the pictures to be displayed
Use a low memory footprint encoding, such as Bitmap.Config.ARGB_4444 more memory than Bitmap.Config.ARGB_8888, such as the 1920*1200 picture.
- Argb_8888:1920*1200*4/1024/1024=8.79mb
- Argb_4444,rgb565:1920*1200*2/1024/1024=4.39mb
Iii. Summary
In Android, the use of the picture must be concerned about, most of the cases, the memory is much, oom occurs because the image resources are not used properly. Do not blindly add a large map to the Android project, can use. 9 come in use, and. 9 The diagram itself is as small as possible, and you can use the drawing implementation without adding an image resource. Sometimes, without affecting the user experience, you can reduce the color quality of the picture, for example, do not need transparency, and some transparency is invisible to the naked eye.
Android efficient memory makes your pictures save memory