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.

In the first project, due to the large number of images and reports loaded by the app, the memory overflow error is often reported, which is a headache. However, under the guidance of the project leader, it is basically a solution. I would like to thank my leader and Samuel. Cai for their hard work. Haha ......
The following is a summary. PS: It was developed from my leader and shared with you. Haha
1. If the project contains a large number of images or the image is too large, oom may occur.
 
Method 1: proportional reduction of images
 
Bitmapfactory. Options = new bitmapfactory. Options ();
Options. insamplesize = 4;

 
Method 2: Use soft references to images for timely recyle () Operations
 
Softreference <bitmap> bitmap;
Bitmap = new softreference <bitmap> (pbitmap );
If (Bitmap! = NULL ){
 
If (bitmap. Get ()! = NULL &&! Bitmap. Get (). isrecycled ()){
Bitmap. Get (). Recycle ();
Bitmap = NULL;
}
}

 
Method 3: properly design and code the complex listview:
1. Pay attention to reuse convertview and holder mechanism in the adapter ----- reference: API demo list 14. Efficient Adapter
 
Public View getview (INT position, view convertview, viewgroup parent ){
If (convertview = NULL ){
V = minflater. Inflate (resource, parent, false );

Final int [] to = MTO;
Final int COUNT = to. length;
Final view [] holder = new view [count];

For (INT I = 0; I <count; I ++ ){
Holder [I] = V. findviewbyid (to [I]);
}

V. settag (holder );
} Else {
}
 
}
2. The above method is not successful yet. Lazy loading data can be used. ----- reference: API demo list 13

 
Method 4: OOM after switching n times on a single page
1. check whether there are large images in the page layout, such as background images. Remove the settings in XML, and set the background image in the Program (in the oncreate () method ):
Drawable BG = getresources (). getdrawable (R. drawable. bg );
Xxx. setbackgrounddrawable (rladdetailone_bg );
Pay attention to BG. setcallback (null) When activity destory; prevent activity from being released in time

 
2. Similar to the above method, directly load the xml configuration file into a view and put it in a container, and then directly call this. setcontentview (view); Method
 
Avoid repeated XML Loading
 

 
Method 5: Use as few code as possible during page switching, such as repeatedly calling the database and repeatedly using some objects ......
 
Method 6: the android heap memory can also be customized and the heap memory allocation of the Dalvik virtual machine can be optimized.

Note: If you use this method, you can only select <= 2.2 For the project build target. Otherwise, the compilation will fail. Therefore, this method is not recommended.
 
Private Final Static int cwj_heap_size = 6*1024*1024;
Private Final Static float target_heap_utilization = 0.75f;
Vmruntime. getruntime (). setminimumheapsize (cwj_heap_size );
Vmruntime. getruntime (). settargetheaputilization (target_heap_utilization );

 

 

 

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.