[Java] <span style = "font-family: Arial, Helvetica, sans-serif;"> <span style = "font-size: 18px; "> </span> do not use setImageBitmap, setImageResource, or BitmapFactory. decodeResource is used to set a large image, because after decode is completed, these functions are finally completed through the createBitmap on the java layer, which consumes more memory. Therefore, use BitmapFactory first. the decodeStream method creates a bitmap and sets it as the source of ImageView. The biggest secret of decodeStream is that it directly calls JNI> nativeDecodeAsset () to complete decode, you no longer need to use the createBitmap on the java layer, thus saving the java Layer Space. If the configuration parameter of the part is added during reading, it can effectively reduce the Memory to be loaded, so as to effectively prevent the throwing out of Memory exception. In addition, decodeStream directly reads the byte code from the image, it does not automatically adapt to various machine resolutions. After decodeStream is used, you need to configure the corresponding image resources in hdpi, mdpi, and ldpi, otherwise, the machine with different resolutions is of the same size (number of pixels), and the displayed size is incorrect. Because after decode is completed, these functions are ultimately completed through the createBitmap on the java layer, and more memory is required. Therefore, use BitmapFactory first. the decodeStream method creates a bitmap and sets it as the source of ImageView. The biggest secret of decodeStream is that it directly calls JNI> nativeDecodeAsset () to complete decode, you no longer need to use the createBitmap on the java layer, thus saving the java Layer Space. If the configuration parameter of the part is added during reading, it can effectively reduce the Memory to be loaded, so as to effectively prevent the throwing out of Memory exception. In addition, decodeStream directly reads the byte code from the image, it does not automatically adapt to various machine resolutions. After decodeStream is used, you need to configure the corresponding image resources in hdpi, mdpi, and ldpi, otherwise, the machine with different resolutions is of the same size (number of pixels), and the displayed size is incorrect. [Java] <span style = "font-size: 18px;"> InputStream is = this. getResources (). openRawResource (R. drawable. pic1); BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = false; options. inSampleSize = 10; // width, hight is set to the original 10-minute Bitmap btp = BitmapFactory. decodeStream (is, null, options); if (! Bmp. isRecycle () {bmp. recycle () // reclaim the memory occupied by the image system. gc () // remind the system to recycle in time} </span> the following method is provided: [java] <span style = "font-size: 18px; ">/*** read images of local resources in the most memory-saving manner ** @ param context * @ param resId * @ return */public static Bitmap readBitMap (Context context, int resId) {BitmapFactory. options opt = new BitmapFactory. options (); opt. inPreferredConfig = Bitmap. config. RGB_565; opt. inPurgeable = true; opt. inInputShare Able = true; // obtain the resource image InputStream is = context. getResources (). openRawResource (resId); return BitmapFactory. decodeStream (is, null, opt );} </span> =============================================== ========================================================== ==== Android memory overflow solution when I put images to gallery on the simulator yesterday, java. lang. outOfMemoryError: bitmap size exceeds VM budget exception. The image size exceeds the RAM memory. The simulator RAM is relatively small and only has 8 MB of memory. When I put a large number of images (about 100 K each), the above problem occurs. Since each image is compressed before, the size will increase when it is placed into Bitmap, resulting in a memory exceeding the RAM. The specific solution is as follows: // solve the problem of memory overflow during image loading. // Options only saves the image size and does not save the image to the memory BitmapFactory. options opts = new BitmapFactory. options (); // scaling ratio. It is difficult to scale according to the prepared ratio. The value indicates the scaling factor. In the SDK, it is recommended that the value be a constant of 2, the larger the value, the image is unclear. inSampleSize = 4; Bitmap bmp = null; bmp = BitmapFactory. decodeResource (getResources (), mImageIds [position], opts );... // reclaim bmp. recycle (); solved by the above method, but this is not the most perfect solution. I learned from some knowledge that the optimization of heap memory allocation of the Dalvik Virtual Machine for the Android platform, the DalvikJava VM used by its hosting layer can be optimized from the current performance, for example, when developing large game or resource-consuming applications, we may consider Manual Interference with GC and use dalvik. system. the setTargetHeapUtilization method provided by the VMRuntime class can improve the processing efficiency of the program heap memory. Of course, we can refer to the open-source project for specific principles. Here we only talk about the usage: private final static float TARGET_HEAP_UTILIZATION = 0.75f; VMRuntime can be called during onCreate. getRuntime (). setTargetHeapUtilization (TARGET_HEAP_UTILIZATION. Android heap memory can also be customized. For some Android projects, the main cause of performance bottleneck is the memory management mechanism of Android. Currently, mobile phone manufacturers are stingy with RAM, for the smoothness of software, RAM is very sensitive to the performance impact. In addition to optimizing the heap memory allocation of the Dalvik virtual machine, we can also forcibly define the memory size of our software, we use the Dalvik provided by dalvik. system. the VMRuntime class sets the minimum heap memory as an example: private final static int CWJ_HEAP_SIZE = 6*1024*1024; VMRuntime. getRuntime (). setMinimumHeapSize (CWJ_HEAP_SIZE); // set the minimum heap memory to 6 MB. Of course, for memory compression, you can also manually interfere with GC to process bitmap and set the image size to avoid memory overflow OutOfMemoryError.★When bitmap is used in android, memory overflow is very easy and the following error is reported: Java. lang. outOfMemoryError: bitmap size exceeds VM budget ● add this section: BitmapFactory. options options = new BitmapFactory. options (); options. inSampleSize = 2; ● eg1: (retrieve images through Uri) private ImageView preview; BitmapFactory. options options = new BitmapFactory. options (); options. inSampleSize = 2; // The image width and height are 1/2 of the original size, that is, the image is 1/4 Bitmap bitmap = BitmapFactory. decodeStream (cr. openInputS Tream (uri), null, options); preview. setImageBitmap (bitmap); The above code can optimize memory overflow, but it only changes the image size and cannot completely resolve memory overflow. ● Eg2: (go to the image through the path) private ImageView preview; private String fileName = "/sdcard/DCIM/Camera/2010-05-14 16.01.44.jpg"; BitmapFactory. options options = new BitmapFactory. options (); options. inSampleSize = 2; // The image width and height are 1/2 of the original size, that is, the image is 1/4 Bitmap B = BitmapFactory. decodeFile (fileName, options); preview. setImageBitmap (B); filePath. setText (fileName );★Android also has some performance optimization methods: ● first, for memory, you can refer to the Android heap memory, you can also customize the size and optimize the heap memory allocation of the Dalvik Virtual Machine ● basic type, because Java does not have actual pointers, it still needs to use NDK for sensitive computation. Android123 prompts game developers. What is interesting is that Google launches NDK to help game developers. For example, the support of OpenGL ES has been significantly improved, and it is necessary to operate the graphical interface of local code. ● Image object optimization. Here we will talk about the destruction of Bitmap objects on Android. You can use the recycle () method to display and let GC reclaim a Bitmap object, generally, the following method can be used for a non-Bitmap, such as if (bitmapObject. isRecycled () = false) // If bitmapObject is not recycled. recycle, currently, the Android system can only preview the first frame of a GIF. You can read Resources in the GIF89 format by using a thread and a self-writing parser in j2s. ● For most Android phones without too many physical buttons, we may need to imagine a good gesture recognition GestureDetector and gravity sensing for control. We usually need to consider noise reduction for misoperation problems.