Android OutOfMemory Solution
In Android development, pay attention to the memory release. Once images are loaded or other resources occupy too much memory, an OOM error occurs, that is, memory leakage. Pay special attention to the release of image resources during development. 1. Release background image and ImageView ------ pay special attention to image resources such:
-
-
- Android: orientation = "vertical"
- Android: background = "@ drawable/main_background"
- Android: id = "@ + id/mian_bg"
- Android: scaleType = "fitXY"
- Android: gravity = "center"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
-
- Android: layout_gravity = "center"
- Android: src = "@ drawable/img_main_roll0"
- Android: id = "@ + id/main_cion"
- Android: layout_width = "180dp"
- Android: layout_height = "180dp"/>
-
-
-
- Obtain the image control first:
- Public ImageView imageView;
- Public LinearLayout linearLayout;
-
- ImageView = (ImageView) findViewById (R. id. main_cion );
- LinearLayout = (LinearLayout) findViewById (R. id. mian_bg );
- Released upon the destruction of the Second Activity
- Protected void onDestroy (){
- Super. onDestroy ();
- ImageView. setImageBitmap (null); // release
- LinearLayout. setBackground (null );
- System. gc (); // collect notifications
- }
-
- Use Bitmap to recall calls for recycling when not in use
- Bitmap. recycle ();
-
-
- Summary:
- Whether you use the following layout in xml:
-
- Android: background,
-
- Or called in java code:
-
- SetBackground (background); ------- API16 +
-
- SetBackgroundDrawable (background) -------- API16-
-
- SetBackgroundResource (resid)
-
- To set the background image.
-
- Call the corresponding method when using it:
- SetBackgroundResource and android: background → setBackgroundResource (0 );
-
- SetBackgroundDrawable (background) → setBackgroundDrawable (null)
-
- SetBackground (background) → setBackground (null)
- Then, onDestory calls System. gc ();
- Copy Code 2. release unused List, array, and other parameters: Obj = null. list first clear (), so that it is equal to null. If the memory is insufficient, you can call Syetem in time. gc () Notification for recovery
|