Android memory overflow Solution

Source: Internet
Author: User

Java. Lang. outofmemoryerror: bitmap size exceeds VM budget exception occurs when gallery is placed in an image on the simulator. 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 was previously compressed. When it is placed into bitmap, the size will increase, resulting in the 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, not the image size to the memory
Bitmapfactory. Options opts = new bitmapfactory. Options ();
// Scale ratio. It is difficult to scale according to the prepared ratio. Its value indicates the zoom factor. In the SDK, it is recommended that its value be the exponential value of 2, the larger the value, the clearer the image.
Opts. insamplesize = 4;
Bitmap BMP = NULL;
BMP = bitmapfactory. decoderesource (getresources (), mimageids [position], opts );

...

// Recycle
BMP. Recycle ();

The above solution is used, but this is not the perfect solution.

For more information, see:

Optimize heap memory allocation of Dalvik virtual machines

For the Android platform, the Dalvik Java 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 open-source projects for specific principles. Here we only talk about the usage: Private Final Static float target_heap_utilization = 0.75f; it can be called during oncreate.
Vmruntime. 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 size 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 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 );
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)
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 );

★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. 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, the following method can be used for a non-bitmap, as shown in figure

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

● 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:

Private Final Static int cwj_heap_size = 6*1024*1024;

Vmruntime. getruntime (). setminimumheapsize (cwj_heap_size); // set the minimum heap memory size 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 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.

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.