Android image processing memory overflow Learning

Source: Internet
Author: User

Recently, I encountered a problem. On the android machine, I called my own camera to get along with each other and scaled, rotated, and intercepted the photos. It seems very simple, however, I encountered a very distressing problem. Here I use the five-way mobile phone for testing. Of course, functional testing is no problem. When I send it to the customer for testing, but the memory overflows...

It turns out that he used Samsung's G3 mobile phone for testing. We didn't have this mobile phone. Later, I used another G3 mobile phone for testing. This problem also exists:

java.lang.OutOfMemoryErrorat android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:518)at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:535)at com.yippeearts.flashcards.CameraPage$1.onPictureTaken(CameraPage.java:73)at android.hardware.Camera$EventHandler.handleMessage(Camera.java:734)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:4514)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)at dalvik.system.NativeStart.main(Native Method)

It is a bit embarrassing to see this error on different mobile phones. I always thought it was a problem with the logic processing of the image. After summing up, I found that it is estimated that the resolution of the photos taken by the G3 mobile phone is too high, so that the picture is too large, resulting in memory overflow during the process, you can search the Internet to solve the problem of memory overflow when loading large images:

Do not use setimagebitmap, setimageresource, or bitmapfactory. decoderesource to set a large image. After decode is completed, these functions are implemented through the createbitmap on the Java layer and consume 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, the load memory can be effectively reduced to prevent the throwing out of Memory exception.
In addition, decodestream reads bytecode directly from images and does not automatically adapt to various machine resolutions. After decodestream is used, it must be in hdpi and mdpi, configure the corresponding image resources in ldpi. Otherwise, the images are of the same size (number of pixels) on machines with different resolutions. The displayed size is incorrect.

Memory overflow solution:

1. 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); // recycles BMP. recycle ();

2. Optimize heap memory allocation for 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 the open-source project for specific principles. Here we only talk about the usage: Private Final Static floattarget_heap_utilization = 0.75f; vmruntime can be called during oncreate. getruntime (). settargetheaputilization (target_heap_utilization.

You can also customize the size of the android heap memory.

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 and are very sensitive to the impact of RAM on the Performance of software fluency, 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 following example uses the vmruntime class to set the minimum heap memory:

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.

Bitmap sets the image size to avoid memory overflow outofmemoryerror.
★When bitmap is used in Android, memory overflow is very easy. The following error is reported: Java. Lang. outofmemoryerror: bitmap size exceeds VM budget

This section is mainly added:

Bitmapfactory. Options = new bitmapfactory. Options ();
Options. insamplesize = 2;

Eg1: (image retrieval through URI)

Private imageview preview; bitmapfactory. options = new bitmapfactory. options (); options. insamplesize = 2; // The image size. The larger the setting, the less unclear the image. The smaller the occupied space, the Bitmap bitmap = bitmapfactory. decodestream (Cr. openinputstream (URI), null, options); preview. setimagebitmap (Bitmap );

Eg2: (image removal Through PATH)

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 );

During image processing, ensure that the image reference is recycled in a timely manner.

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.