Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set a large image, because these functions are completed decode, and ultimately through the Java layer of CreateBitmap to complete, you need to To consume more memory.
Therefore, instead of using the Bitmapfactory.decodestream method first to create a bitmap, and then set it to ImageView Source,decodestream the biggest secret is its direct call jni>> Nativedecodeasset () to complete the decode, eliminating the need to use the Java layer CreateBitmap, thus saving the Java layer of space.
If the config parameter is added to the read, it can be used to reduce the loaded memory effectively, thereby effectively blocking the throw out of
In addition, Decodestream directly take the picture to read the bytecode, not according to the machine's various resolutions to automatically adapt, after using the Decodestream, you need to configure the corresponding image resources in hdpi and mdpi,ldpi. Otherwise the same size (number of pixels) is the same on different resolution machines, and the displayed size is wrong.
Memory Overflow Workaround:
1, the simulator RAM is relatively small, only 8 m of memory, when I put a large number of pictures (each more than 100 k), the above reasons appear. Since each picture was previously compressed, when placed into the bitmap, the size will become larger, resulting in more than RAM memory, the specific solution is as follows:
// solve the problem of loading picture memory overflow // Options only save image size, do not save image to memory New bitmapfactory.options (); // Scaling, scaling is difficult to scale by the prepared scale, its value indicates the scale of the multiplier, the SDK recommended that its value is 2 of the exponential value, the more the value of the general cause the picture is not clear Opts.insamplesize = 4; NULL ; = Bitmapfactory.decoderesource (getresources (), mimageids[position], opts); // Recycling
2. Optimize heap memory allocation for Dalvik virtual machines
For Android platforms, the Dalvik JAVAVM used by its hosting layer has many things to optimize for its current performance, such as the possibility of manually interfering with GC processing in the development of some large-scale games or resource-consuming applications. Using the Settargetheaputilization method provided by the Dalvik.system.VMRuntime class can enhance the processing efficiency of the program heap memory. Of course, we can refer to the specific principle of open source engineering, here we only say how to use: Private final static floattarget_heap_utilization = 0.75f; You can call Vmruntime.getruntime (). Settargetheaputilization (Target_heap_utilization) when the program OnCreate.
Android heap memory can also define their own size, for some Android projects, the main impact of performance bottleneck is Android's own memory management mechanism problem, mobile phone manufacturers are more stingy with RAM, for the smoothness of the software, Ram has a very sensitive impact on performance.
Private Final Static int Cwj_heap_size = 6* 1024* 1024x768 ; //
Bitmap setting picture size to avoid memory overflow OutOfMemoryError optimization method
★android is easy to use when using bitmap memory overflow, reported the following error: Java.lang.OutOfMemoryError:bitmap size exceeds VM budget
The main addition is this paragraph:
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.insamplesize = 2;
EG1: (Take a picture by URI)
Private ImageView preview; New bitmapfactory.options (); = 2; // picture size, the larger the setting, the less clear the picture, the smaller the space occupied NULL , options); Preview.setimagebitmap (bitmap);
EG2: (Through the path to the picture)
Private ImageView preview; Private String filename= "/sdcard/dcim/camera/2010-05-14 16.01.44.jpg"; New bitmapfactory.options (); = 2; // The image width is the original One-second, that is, the picture is the original One-fourth Bitmap B = bitmapfactory.decodefile (fileName, options); Preview.setimagebitmap (b);
In the picture processing, make sure the picture reference is collected in time.
Android Image processing Memory overflow learning