Processing of Bitmap memory overflow in Android

Source: Internet
Author: User

★When bitmap is used in Android, memory overflow is very easy and the following error is reported:
Java code
Java. Lang. outofmemoryerror: bitmap size exceeds VM budget
● This section is mainly added:
Java code
Bitmapfactory. Options = new bitmapfactory. Options ();
Options. insamplesize = 2;
Bitmapfactory. Options = new bitmapfactory. Options (); options. insamplesize = 2;
● Eg1: (image retrieval through URI)
Java code
Private imageview preview;
Bitmapfactory. Options = new bitmapfactory. Options ();
Options. insamplesize = 2; // The image width and height are 1/2 of the original image, that is, the image is 1/4 of the original image.
Bitmap bitmap = bitmapfactory. decodestream (Cr
. Openinputstream (URI), null, options );
Preview. setimagebitmap (Bitmap );

Private imageview preview; bitmapfactory. 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
. Openinputstream (URI), null, options); preview. setimagebitmap (Bitmap );
The above code can optimize memory overflow, but it only changes the image size and does not completely resolve the memory overflow.
● Eg2: (image removal Through PATH)
Java code
Private imageview preview;

Private string filename = "/sdcard/dcim/camera/2010-05-14 16.01.44.jpg ";
Bitmapfactory. Options = new bitmapfactory. Options ();
Options. insamplesize = 2; // The image width and height are 1/2 of the original image, that is, the image is 1/4 of the original image.
Bitmap B = bitmapfactory. decodefile (filename, options );
Preview. setimagebitmap (B );
Filepath. settext (filename );

Private imageview preview; private string filename = "/sdcard/dcim/camera/2010-05-14 16.01.44.jpg"; bitmapfactory. 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 );
★There are also some Performance Optimization Methods for Android:
● In terms of memory, you can refer to the android heap memory and customize the size and optimize the heap memory allocation of the Dalvik virtual machine.
● In terms of basic types, Because Java does not have actual pointers, ndk must be used for sensitive operations. What is interesting about this 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 GUI on the 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, the following method can be used for a non-bitmap, as shown in figure
Java code

If (bitmapobject. isrecycled () = false) // if not recycled
Bitmapobject. Recycle ();

If (bitmapobject. isrecycled () = false) // If bitmapobject. Recycle () is not recycled ();
● Currently, the system is relatively mentally retarded in supporting animation, which can be used for the interim Effect of regular applications. However, for games, normal artists may be used to unified GIF Processing, 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.
You can also customize the size of the android heap memory.
For some large Android projects or games, there is no problem in algorithm processing. The main cause of the 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. Apart from the optimization of the heap memory allocation of the Dalvik Virtual Machine mentioned in the android Development Network last time, we can also forcibly define the memory size of our software. We use the Dalvik provided by Dalvik. system. the following example uses the vmruntime class to set the minimum heap memory:
Java code
Private
Final
Static
Int cwj_heap_size = 6x1024*1024;
Vmruntime. getruntime (). setminimumheapsize (cwj_heap_size); // set the minimum heap memory size to 6 MB.
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, manual GC interference can also be performed. We will refer to the specific application next time.
Optimize heap memory allocation of Dalvik virtual machines
For the Android platform, the Dalvik JavaVM 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 open-source projects for specific principles. Here we will only talk about the usage:
Java code
Private
Final
Static floattarget_heap_utilization = 0.75f;
Private Final Static floattarget_heap_utilization = 0.75f;
You can call
Vmruntime. getruntime (). settargetheaputilization (target_heap_utilization );
Vmruntime. getruntime (). settargetheaputilization (target_heap_utilization.

When the Android system reads bitmap, the stack size of the image to be allocated to the virtual machine is only 8 Mb. So no matter how the image is called, too many large virtual machines will certainly report that error. Error in memory budget exceeding image size: Java. Lang. outofmemoryerror: bitmap size exceeds VM budget

This problem occurs because resources are not recycled.
1 Public void distorybitmap (){
2 If (null! = BMB &&! BMB. isrecycled ())
3 BMB. Recycle ();
4}

The above code can basically solve this problem, but do not call it in ondraw () in the view. Because the ondraw () method is called cyclically by the system, as long as the image is opened.
The system keeps calling this method.
The best solution is to add a header call to the init () initialization method in the Custom view. Or call it at the top of the constructor.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.