SoFunction
Updated on 2025-03-02

Android memory optimization picture optimization

Operation on the picture itself. Try not to use setImageBitmap, setImageResource, to setImageResource, because after these methods are completed, they are eventually completed through the Java layer createBitmap, which requires more memory consumption. Therefore, instead, use the method to create a bitmap, and then set it to the source of ImageView. The biggest secret of decodeStream is that it directly calls JNI>>nativeDecodeAsset() to complete the decode, and there is no need to use the createBitmap of the java layer, thus saving the space of the java layer. If the image's Config parameter is added when reading, it can more effectively reduce the loaded memory, thereby more effectively preventing the throwing of memory exceptions. In addition, decodeStream directly uses images to read bytecode, and will not automatically adapt according to the various resolutions of the machine. After using decodeStream, corresponding image resources need to be configured in hdpi, mdpi, and ldpi. Otherwise, the same size (number of pixels) on machines with different resolutions, and the displayed size is incorrect.

Copy the codeThe code is as follows:

InputStreamis=().openRawResource(.pic1);
=();
=false;
=10;//width, set to the original ten-one
Bitmapbtp=(is,null,options);

Copy the codeThe code is as follows:

if(!()){
()//Recycle the memory occupied by the picture
()//Remind the system to recover in a timely manner
}

Copy the codeThe code is as follows:

/**
*Read pictures of local resources in the most memory-saving way
*@paramcontext
*@paramresId
*@return
*/
publicstaticBitmapreadBitMap(Contextcontext,intresId){
=();
=.RGB_565;
=true;
=true;
//Get resource pictures
InputStreamis=().openRawResource(resId);
(is,null,opt);
}

The value in option refers to the scale of the image. The SDK recommends that its value is 2 exponential value. The larger the value, the less clear the image. The length and width are only 1/2 of the original picture. As the image size decreases, the memory consumed naturally becomes smaller. The disadvantage of doing this is that the image quality becomes worse. The larger the value of inSampleSize, the worse the image quality. Due to the different algorithms of zooming pictures for each mobile phone manufacturer, the quality of zooming pictures on different mobile phones may be different. The author has encountered the situation where the quality of the picture on a moto phone is acceptable after scaling, but the same scaling ratio on a Samsung phone is much worse.

There are four types of Android, namely
ALPHA_8: Each pixel takes up 1 byte memory
ARGB_4444: Each pixel takes up 2byte memory
ARGB_8888: Each pixel occupies 4byte memory
RGB_565: Each pixel takes up 2byte memory
The default color mode for Android is ARGB_8888. This color mode has the most delicate colors and the highest display quality. But again, the memory consumes the largest amount.
The above code will be read in ARGB_4444 mode. Although the memory reduction is not as obvious as the first method, for most pictures, there is no difference between the ARGB_8888 mode. However, when reading pictures with gradient effects, color bars may appear. In addition, it will affect the special effects processing of the picture.
Optimize the heap memory allocation of Dalvik virtual machines. For Android platform, the DalvikJavaVM used in its hosting layer still has many places to optimize processing based on its current performance. For example, when developing some large games or resource-consuming applications, we may consider manually interfering with GC processing. Using the setTargetHeapUtilization method provided by the class can enhance the processing efficiency of program heap memory. How to use:
Copy the codeThe code is as follows:

privatefinalstaticfloatTARGET_HEAP_UTILIZATION=0.75f;
().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

Just do it.
Also, it is possible to define the size of the heap memory.
Copy the codeThe code is as follows:

privatefinalstaticintCWJ_HEAP_SIZE=6*1024*1024;().setMinimumHeapSize(CWJ_HEAP_SIZE);//Set the minimum heap memory to 6MB size

Call the recycle() method of the picture:
This is not actually a way to really reduce the memory of the image. The main purpose is to mark image objects to facilitate the recycling of local data of image objects. The local data of the image object occupies the largest memory and is calculated separately from the Java part of the program. Therefore, Javaheap is often used enough and the image has OutOfMemoryError. Calling this method when the image is not in use can effectively reduce the peak value of the image local data, thereby reducing the probability of OutOfMemoryError. However, the image object that calls recycle() is in the "deprecated" state, which will cause program errors when called. Therefore, if it is impossible to guarantee that the image object will never be called again, it is not recommended to use this method. Pay special attention to already usedsetImageBitmap(Bitmapimg)The image object assigned to the control,May be called by the system library,Causes program errors。

How to change color mode for image enlarged using Matrix objects
Although using Matrix objects to enlarge images will inevitably consume more memory, sometimes you have to do this. The ARGB_8888 color mode used in the enlarged picture, even if the original picture is ARGB_4444 color mode, and there is no way to directly specify the color mode when enlarging. The following methods can be used to change the image color mode.
The code is as follows
Copy the codeThe code is as follows:

Matrixmatrix=newMatrix();
floatnewWidth=200;//The width after enlarged picture
floatnewHeight=300;//The length after enlarged picture
(newWidth/(),newHeight/());
Bitmapimg1=(img,0,0,(),(),matrix,true);//Get the enlarged picture
img2=(.ARGB_4444,false);//Get the image of ARGB_4444 color mode
img=null;
img1=null;

Here, an additional image object img1 is generated compared to the original image. However, the system will automatically recycle img1, so the actual memory is still reduced.