Android solves memory overflow problems
1. When the project contains a large number of images or the picture is too large Method 1: proportional reduction of images
- BitmapFactory. Options options = new BitmapFactory. Options ();
- Options. inSampleSize = 4
- Copy code Method 2: Use soft references to images and perform recyle () operations in a timely manner
-
- SoftReference Bitmap;
- Bitmap = new SoftReference (PBitmap );
- If (bitmap! = Null ){
- If (bitmap. get ()! = Null &&! Bitmap. get (). isRecycled ()){
- Bitmap. get (). recycle ();
- Bitmap = null;
- }
- }
- Copy code method 3: Design and code the complex listview properly (I personally think this is a reliable point) 1. Pay attention to reuse convertView In the Adapter and the use of the holder Mechanism
----- Reference: api demo list 14. Efficient Adapter
2. The above method has not been successfully tried. lazy loading data can be used. ----- 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 {
- }
- }
- Code Copying 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 );
- When copying code in Activity destory, note that bg. setCallback (null); prevent the 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 view) to avoid repeated xml loading.
Method 5: Use as few code as possible during page switching. For example, repeatedly calling the database and repeatedly using some objects .....
Method 6: the Android heap memory can also be customized and the memory of the Dalvik virtual machine can be optimized. -- Reference: http://blog.csdn.net/wenhaiyan/article/details/5519567
- Note: If this method is used, only version <= 2.2 can be selected for project build target. Otherwise, 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 );
- Copy code
|