How to solve Android oom

Source: Internet
Author: User

Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set a larger picture.
Since these functions are finished decode, finally all through the Java layer of CreateBitmap to complete, need to consume a lot of other memory.


Therefore, instead of using the Bitmapfactory.decodestream method first, create a bitmap, and then set it to ImageView source.
Decodestream's biggest secret lies in its direct call to Jni>>nativedecodeasset () to complete the decode.
No longer need to use Java layer createbitmap. This saves the Java layer of space.


Assuming that the config parameter is added to the read, it is able to reduce the loaded memory effectively, thereby effectively blocking the throw out of
In addition, Decodestream directly take the picture to read the bytecode. Does not adapt itself to the various resolutions of the machine,
After using the Decodestream. You need to configure the corresponding picture 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 not correct.


Other than that. The following methods are also helpful:
1. 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 very
Bitmap BTP =bitmapfactory.decodestream (is,null,options);
2. if (!bmp.isrecycle ()) {
Bmp.recycle ()//Recover the memory occupied by the picture
System.GC ()//Alarm system timely recovery
}


Here's a method:


Java code


1./**
2. * Read pictures of local resources in the most memory-saving way
3. * @param context
4. * @param resId
5. * @return
6. */
7. public static Bitmap Readbitmap (context context, int resId) {
8. Bitmapfactory.options opt = new bitmapfactory.options ();
9. opt.inpreferredconfig = Bitmap.Config.RGB_565;
Ten. Opt.inpurgeable = true;
One. Opt.ininputshareable = true;
12.//Get a picture of a resource
InputStream is = Context.getresources (). Openrawresource (ResId);
Bitmapfactory.decodestream return (is,null,opt);
15.}




================================================================================
How to resolve an Android memory overflow


Transferred from: Http://www.cppblog.com/iuranus/archive/2010/11/15/124394.html?opt=admin


When I put a picture on gallery yesterday in the simulator. The Java.lang.OutOfMemoryError:bitmap size exceeds VM budget exception appears, and the image size exceeds RAM memory.
Simulator RAM is smaller and only has 8 m of memory. When I put in a large number of pictures (about 100 or so more than k), there is the reason above.
Because each picture was previously compressed. When put into the bitmap, the size will become larger, resulting in more than RAM memory, the method of detailed resolution such as the following:


Resolves an issue with loading a picture memory overflow
Options just save the image size, do not save the image to memory
Bitmapfactory.options opts = new Bitmapfactory.options ();
The scale of the scale. Scaling is very difficult to scale at the prepared scale, and its value indicates a multiple of scaling. In the SDK, it is recommended that the value is 2 index value, the higher the value of the general cause the picture is not clear
Opts.insamplesize = 4;
Bitmap bmp = null;
BMP = Bitmapfactory.decoderesource (Getresources (), mimageids[position],opts);


...


Recovery
Bmp.recycle ();


This is not the perfect solution, but it's the best way to overcome it.




Through some understanding, know for example the following:


Optimize heap memory allocation for Dalvik virtual machines


For the Android platform, the Dalvik Java VM used by its hosting layer has a lot more to do with the 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 the detailed principle we can participate in open source project, here we only say usage: Private final static float target_heap_utilization = 0.75f; You can call Vmruntime.getruntime () when the program OnCreate. Settargetheaputilization (target_heap_utilization); We can.






Android heap memory can also be customized size


For some Android projects. The main problem affecting performance bottlenecks is Android's own memory management mechanism. At the moment, mobile phone manufacturers are more stingy with RAM, for software fluency, RAM is very sensitive to performance, in addition to optimizing the heap memory allocation of Dalvik virtual machines, we can also force to define their own software memory size. We use the Dalvik.system.VMRuntime class provided by Dalvik to set the minimum heap memory as an example:


Private final static int cwj_heap_size = 6* 1024* 1024;


Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); Sets the minimum heap memory size to 6MB. Of course, the memory crunch can also be handled by manually interfering with the GC.




Bitmap set the picture size. Optimization method to avoid memory overflow outofmemoryerror
Very easy memory overflow when using bitmap in ★android, such as 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;
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.insamplesize = 2;//picture width is one of the original two points, that is, the picture is one of the original four points
Bitmap Bitmap = Bitmapfactory.decodestream (cr
. Openinputstream (URI), null, options);
Preview.setimagebitmap (bitmap);
The above code can optimize memory overflow. But it simply changes the size of the image and does not completely resolve the memory overflow.
EG2: (Through the path to the picture)
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;//picture width is one of the original two points, that is, the picture is one of the original four points
Bitmap B = Bitmapfactory.decodefile (fileName, Options);
Preview.setimagebitmap (b);
Filepath.settext (FileName);


★android other ways to optimize performance:
First memory aspect, able to reference Android heap memory can also customize size and optimize heap memory allocation for Dalvik virtual machines


On the underlying type, there is no actual pointer to Java. In terms of sensitive operations, the NDK is still needed. Android123 Tips Game developers. The interesting thing about this is that Google's launch of the NDK may be helping game developers. The support for OpenGL ES, for example, has changed significantly. The local code operation graphical interface is very necessary.




Graphical object optimization. This is to say that the bitmap object is destroyed on Android, and the Recycle () method can be used to display the GC to reclaim a bitmap object. It is usually possible to use the following methods for a bitmap. Such as


if (bitmapobject.isrecycled () ==false)//Assuming no recycling
Bitmapobject.recycle ();


Now the system for the animation support is more mentally for the general application of the tween transition effect can, but for the game general art may be accustomed to the GIF way of unified processing, now the Android system can only preview the first frame of GIF, Resources in the GIF89 format can be read in J2ME by threading and writing parsers themselves.




For most Android phones there are not too many physical keys that we might need to imagine to do with gesture recognition gesturedetector and gravity sensing for manipulation. In general, we also consider the noise reduction process for misoperation problems.


Android heap memory can also be customized size


There is no problem with algorithmic processing for some large Android projects or games. The main impact of the performance bottleneck is Android's own memory management mechanism, the handset manufacturers are stingy with RAM, for the smoothness of software, RAM is very sensitive to performance, in addition to the last time the Android development network mentioned the optimization of Dalvik virtual machine heap memory allocation. We are also able to enforce the memory size of our own software, and we use the Dalvik.system.VMRuntime class provided by Dalvik to set the minimum heap memory as an example:


Private final static int cwj_heap_size = 6* 1024* 1024;


Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); Sets the minimum heap memory size to 6MB.

Of course the memory crunch can also be handled by manually interfering with the GC, which we'll refer to in detail next time.


Optimize heap memory allocation for Dalvik virtual machines


For the Android platform, the Dalvik JAVAVM used by its hosting layer has a very large number of places to optimize processing from the present performance, for example, we may consider manual interference in 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 open source project for detailed principles. Here we only say usage: private final static floattarget_heap_utilization = 0.75f; You can call Vmruntime.getruntime () when the program OnCreate. Settargetheaputilization (target_heap_utilization); We can.


Introduce a memory algorithm that occupies a process.


The underlying class for working with pictures in Android is bitmap. As the name implies, it is a bitmap. The memory-hogging algorithm is as follows:
The width*height*config of the picture.
Assume that config is set to argb_8888. Then the above config is 4. A 480*320 picture takes up memory that is 480*320*4 byte.
Someone in front said the concept of 8M, in fact, by default, the Android process memory consumption is 16M, because bitmap he holds data in addition to Java, the underlying C + + Skia graphics library will also hold a Skbitmap object. Therefore the general picture occupies memory recommended size should not exceed 8M. This can be adjusted. You can set the number of parameters when compiling the source code.

How to solve Android oom

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.