Android memory overflow solution (vmruntime. getruntime (). setminimumheapsize (cwj_heap_size );

Source: Internet
Author: User

 

From: http://www.apkbus.com/forum.php? MoD = viewthread & tid = 898.

On the simulatorGalleryWhen the image is placedJava. Lang. outofmemoryerror: bitmap size exceeds VM budgetException: The image size exceeds the RAM memory.
SimulatorRamRelatively small, only 8 M memory, when I put a large number of images (each more than 100 K), the above reasons appear. Since each image was previously compressed. ToBitmapWhen the RAM memory is exceeded, the size will increase. The specific solution is as follows:

Java code:

// 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,Dalvik Java VMFrom the current performance, there are still many areas to optimize processing. For example, we may consider Manual Interference in GC processing when developing large game or resource-consuming applications.
Dalvik. system. vmruntimeClassSettargetheaputilizationThe method can enhance 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:
Private Final Static float target_heap_utilization = 0.75f;In the programOncreateYou can callVmruntime. getruntime (). settargetheaputilization (target_heap_utilization );You can.

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 heap memory size of our software. We useDalvik. system. vmruntimeFor example:

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.

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.