When doing memory optimization, we found that in addition to solving the memory leak problem, the only remaining solution is to reduce the real memory footprint. In the App, most of the memory may be occupied by our image, so reducing the memory footprint of the image can have a direct effect.
First, how much memory does a picture occupy?Let's assume we have a picture that is 600 * 800 pixels, and the picture disk space is assumed to be 100KB.
What is the relationship between the size of the image memory and the size of the disk space?
The space occupied by the disk is not the size of the memory occupied by the image. The space occupied by the disk is a space required for storing the image on the disk, and the memory size is the memory occupied by the memory loaded into the memory. Two units are the same, and the essence is not a concept.
How much memory does a picture occupy?
The calculation formula for the memory occupied by the picture is: picture height * picture width * The memory size occupied by one pixel. In Android, the default pixel occupied memory is 4 bytes, so the above picture takes up memory: 800 * 600 * 4 byte = 1875KB = 1.83M. Why is it 4 bytes? Must be 4 bytes? These two questions are followed carefully.
What is the effect on the memory of the directory where the picture is located?
In Android, the storage directory of the pictures and the screen density of the mobile phone affect the actual size of the pictures that are finally loaded into the memory. For example: Suppose our pictures are placed in the xhdpi directory. The memory size of the pictures in this article is as follows:
• Equipment with screen density of 2: 800 * 600 * 4byte = 1.83M
• Equipment with a screen density of 3: 800 * 1.5 * 600 * 1.5 * 4byte = 1.83 * 2.25M = 4.12M
• The screen density mentioned here refers to the density variable in the android.uTIl.DisplayMetrics class, which is a float value. This article does not describe the screen density.
Therefore, when calculating the memory size of a picture, it is necessary to consider the directory where the picture is located and the screen density. These two factors actually affect the height and width of the picture. Android will pull up and compress the picture.
Second, let your picture save memory 2.1 Minimize your pictureThe memory usage of the picture is calculated as: picture height* picture width* The memory size occupied by one pixel, so if the height and width of the picture are both doubled from the original width and height, then the memory will be quadrupled. So the principle of use of the picture can be summarized as follows:
1. Use as small a picture as possible
2. Using the .9 diagram, the .9 diagram itself should be as small as possible
3. Draw yourself (override the onDraw of your View) or use Drawable to draw
For example, to achieve a linear gradient can use the following drawable to achieve:
2.2 Compressing pictures in memoryWhen you load a large image, you need to compress the image and use an equal-proportional compression method to process the image directly in memory.
OpTIons opTIons = new BitmapFactory.OpTIons(); options.inSampleSize = 5; // One-fifth of the original image, set to 2 for one-half Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id. Myimage, options);
What you should pay attention to is that the picture quality will be degraded, and the larger the value of inSampleSize is, the worse the picture quality will be.
2.3 Do not load pictures into memory when reading bitmap size and typeSometimes we get a picture, perhaps just to get some information of this picture, such as the picture width, height and other information, do not need to display to the interface, this time we can not load the picture into 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 Recovered after useSince the outer layer of Android uses java, the bottom layer uses the memory space allocated by the C language for the picture object. Therefore, although our external appearance seems to be released, the inner layer is not necessarily completely released. After we use the picture, it is better to release the internal memory space.
If (!bitmapObject.isRecyled()) { // Bitmap object is not being recycled bitmapObject.recycle(); // Release System.gc(); // Remind the system to recycle promptly}
2.5 Reduce the color quality of the picture to be displayed
2.5.1 Color ModelRGB (ARGB)
RGB color mode is a color standard in the industry. It is obtained by changing the three color channels of red (R), green (G), and blue (B), and superimposing them with each other to obtain various colors. RGB is the color representing the three channels of red, green and blue. This standard includes almost all colors that can be perceived by human vision. It is one of the most widely used color systems. There is also a color model containing transparency Alpha in Android, namely ARGB.
2.5.2 Digital encoding of RGB color values ​​in a computerWithout considering the transparency, the color value of a pixel can be represented in the computer in the following three ways:
1. Floating-point number encoding: For example, float: (1.0, 0.5, 0.75). Each color component occupies one float field, and 1.0 indicates that the value of the component is all red or all green or all blue.
2. 24-bit integer encoding: For example, 24-bit: (255, 128, 196). Each color component occupies 8 bits. The value range is 0-255. 255 indicates that the value of the component is all red or all green. Or all blue.
3, 16-bit integer encoding: For example, 16-bit: (31, 45, 31), the first and third color components each occupy 5, the range of 0-31, the second color component 6-bit, The value ranges from 0-63.
In Java, variables of type float are 32 bits, variables of type int are 32 bits, variables of types short and char are all in bits of 16, so it can be seen that the color of a pixel is encoded in floating-point representation and the memory is occupied. It is 96 bits, ie 12 bytes; while encoding with 24-bit integer notation, as long as an int type variable, it occupies 4 bytes (high 8 bits are empty, low 24 bits are used to represent colors); use 16-bit integer representation Encoding, as long as a short type variable, occupies 2 bytes; therefore we can see that the use of integer representation encoding color values, can greatly save memory, of course, the color quality will be relatively low. Bitmaps in Android generally use integer encoding.
2.5.3 RGB encoding format in Android (Integer encoding)
• RGB888(int): 8 bits each for R, G, and B components
• RGB565 (short): R, G, and B components occupy 5, 6, and 5 bits, respectively
• RGB555 (short): RGB components are represented by 5 bits (the remaining 1 bit is not used)
• ARGB8888(int): 8 bits each of A, R, G, and B components
• ARGB4444 (short): A, R, G, and B components each occupy 4 bits
In Android's Bitmap.Config class, there are ARGB_8888, ARGB_4444, RGB565, and other constants, and you can now know what they mean.
In Android, the default encoding format used by the system is ARGB_8888, so when the memory size is calculated at the beginning of the article, the memory size of 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 Reducing Color Quality of Images to DisplayUse a low-memory-intensive encoding, such as Bitmap.Config.ARGB_4444, which saves more memory than Bitmap.Config.ARGB_8888, such as a 1920*1200 image.
• ARGB_8888: 1920*1200*4/1024/1024=8.79MB
• ARGB_4444, RGB565: 1920*1200*2/1024/1024=4.39MB
Third, summaryIn Android, we must pay attention to the use of pictures. In most cases, it takes up more memory, and OOM occurs because the picture resources are used improperly. Do not blindly add a big picture to your Android project. You can use .9 to use it, and the .9 picture itself should be as small as possible. In addition, you can use the drawing implementation without adding a picture resource. Sometimes, without affecting the user experience, the color quality of pictures can be reduced. For example, transparency is not required. Some transparency is invisible to the naked eye.
The hydrogel Screen Protector has super ductility and shrinkage, has a strong and effective self-repair function, impact resistance, durability, better toughness, and has a certain buffering effect on sharp objects. The use of hydrogel film can adapt to the contours of any device, so it can be attached to curved screens and rounded edges. The full-coverage screen protector can perfectly fit your screen and provide maximum protection.
The 0.14mm ultra-thin thickness is more sensitive to the touch, and the ultra-thin design gives you a bare-metal experience. The oleophobic and waterproof coating prevents fingerprints and dust, makes the hydrogel screen protector easy to clean, and makes your phone look more beautiful.
Hd Clear Hydrogel Protector Sheet,Anti Blue-Ray Hydrogel Protector,Matte Hydrogel Film,Privacy Hydrogel Film
Shenzhen TUOLI Electronic Technology Co., Ltd. , https://www.szhydrogelprotector.com