Several code handling for Android memory optimization

Source: Internet
Author: User

The following are some of the Androi memory Optimization code processing, sample code, for reference only:

? 1. Generate Bitmap:

InputStream is =this.getresources (). Openrawresource (R.DRAWABLE.PIC1);
bitmapfactory.options options=newbitmapfactory.options ();
Options. Injustdecodebounds = false;
Options.   insamplesize = 10; Width,hight is set to the original very
Bitmap BTP =bitmapfactory. Decodestream (is,null,options);
2. Recycling Bitmap:

if (!bmp.isrecycle ()) {
Bmp.recycle ()//Recover the memory occupied by the picture
System.GC ()//Alarm system timely recovery
}
Call Recycle (), it will open a thread to handle, when exactly when recycling only the system knows. (belongs to the recommended JVM for garbage collection)
This is why we have a place to call it, and that place will still be the reason to report oom.
? But there is always better than nothing, it can effectively reduce the probability of oom.
Recycling Resources
(1)//cache for storing images
Hashmap<integer, Bitmap>bitmapcache = new Hashmap<integer, bitmap> ();
If there is no picture, or already exists
if (Bitmapcache.isempty () | | |! AppConst.bitmapCache.containsKey (position)) {
Bitmapcache.put (POSITION,BM);
}
(2) Define the FREEBITMAP function, call the Freebitmap function at the end of the activity, and reclaim the resources in the map
private void Freebitmap (Hashmap<integer, bitmap> cache) {
if (Cache.isempty ()) {
Return
}
For (Bitmap bitmap:cache.values ()) {
if (bitmap! = null &&!bitmap.isrecycled ()) {
Bitmap.recycle ();
}
}
Cache.clear ();
}

3. Manually intervene heap memory:

(1) Enhance the processing efficiency of the program heap memory:

You can consider manually interfering GC processing when developing some large game or resource-consuming applications, such as calling Vmruntime.getruntime () when the program OnCreate. settargetheaputilization (0.75f);

(2) Force the app to set the minimum memory size:

Vmruntime.getruntime (). setminimumheapsize (6* 1024* 1024); Set minimum heap memory to 6MB size

Several code handling for Android memory optimization

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.