Android OutOfMemory Solution

Source: Internet
Author: User

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:
  1. Android: orientation = "vertical"
  2. Android: background = "@ drawable/main_background"
  3. Android: id = "@ + id/mian_bg"
  4. Android: scaleType = "fitXY"
  5. Android: gravity = "center"
  6. Android: layout_width = "fill_parent"
  7. Android: layout_height = "fill_parent"
  8. >
  9. Android: layout_gravity = "center"
  10. Android: src = "@ drawable/img_main_roll0"
  11. Android: id = "@ + id/main_cion"
  12. Android: layout_width = "180dp"
  13. Android: layout_height = "180dp"/>
  14.  
  15.  
  16. Obtain the image control first:
  17. Public ImageView imageView;
  18. Public LinearLayout linearLayout;
  19.  
  20. ImageView = (ImageView) findViewById (R. id. main_cion );
  21. LinearLayout = (LinearLayout) findViewById (R. id. mian_bg );
  22. Released upon the destruction of the Second Activity
  23. Protected void onDestroy (){
  24. Super. onDestroy ();
  25. ImageView. setImageBitmap (null); // release
  26. LinearLayout. setBackground (null );
  27. System. gc (); // collect notifications
  28. }
  29.  
  30. Use Bitmap to recall calls for recycling when not in use
  31. Bitmap. recycle ();
  32.  
  33.  
  34. Summary:
  35. Whether you use the following layout in xml:
  36.  
  37. Android: background,
  38.  
  39. Or called in java code:
  40.  
  41. SetBackground (background); ------- API16 +
  42.  
  43. SetBackgroundDrawable (background) -------- API16-
  44.  
  45. SetBackgroundResource (resid)
  46.  
  47. To set the background image.
  48.  
  49. Call the corresponding method when using it:
  50. SetBackgroundResource and android: background → setBackgroundResource (0 );
  51.  
  52. SetBackgroundDrawable (background) → setBackgroundDrawable (null)
  53.  
  54. SetBackground (background) → setBackground (null)
  55. Then, onDestory calls System. gc ();
  56. 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

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.