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