Eoeandroid Recommended: Android avoids memory overflow Http://www.eoeandroid.com/thread-180008-1-1.html Workarounds for Android Memory Overflow Http://www.eoeandroid.com/thread-66415-1-1.html
1. When the project contains a large number of pictures, or the picture is too large Method 1: Zoom out of the picture
- Bitmapfactory.options Options = new Bitmapfactory.options ();
- Options.insamplesize = 4
Copy CodeMethod 2: Take a soft reference to the picture and do the Recyle () operation in a timely manner
- Softreference<bitmap> Bitmap;
- Bitmap = new softreference<bitmap> (PBITMAP);
- if (bitmap! = null) {
- if (bitmap.get () = null &&!bitmap.get (). isRecycled ()) {
- Bitmap.get (). Recycle ();
- bitmap = null;
- }
- }
Copy CodeMethod 3: Rational design and coding of complex ListView (personal feeling this point) 1. Pay attention to reusing the Convertview in adapter and the use of holder mechanism ---Reference: API Demo list 14. Efficient Adapter
2. The above method attempt has not been successful and can be used with the lazy loading data --- Reference: API Demo List 13
- 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 {
- }
- }
Copy CodeMethod 4: Single page, toggle N-Time after OOM on screen 1. See if there are any large pictures in the page layout, such as the background map. Remove the relevant settings from the XML and set the background map in the program (in the OnCreate () method):
- drawable bg = getresources (). getdrawable (r.drawable.bg);
- Xxx.setbackgrounddrawable (RLADDETAILONE_BG);
Copy Codebe aware of the activity destory, bg.setcallback (null) and prevent the activity from being released in a timely manner. 2. Similar to the above method, directly load the XML configuration file into a view and put it into a container, and then call This.setcontentview (view view) directly, avoid the repeated loading of XML.
Method 5: Reuse as few code as possible while the page is switching. For example: Repeated calls to the database, repeated use of certain objects, etc. ..
method 6:android Heap memory can also define its own size and optimize the memory of the Dalvik virtual machine -- Reference:http://blog.csdn.net/wenhaiyan/article/details/5519567
- Note: If you use this method: project build target can only select the <= 2.2 version, otherwise the compilation will not pass. So it is not recommended in this way.
- 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);
Copy Code |
Address: http://www.eoeandroid.com/thread-198865-1-1.html [Copy link]
android resolves memory overflow issues