溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android的OutOfMemory解決

發布時間:2020-07-29 18:17:20 來源:網絡 閱讀:360 作者:wufanxin 欄目:移動開發

安卓開發中應注意內存的釋放,一旦加載圖片或其他占用太多內存,此時就會發生OOM錯誤,即內存泄露。


在開發中,尤其應注意圖片資源的釋放。

1。背景圖片和ImageView釋放------尤其注意圖片資源

如:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3.               android:orientation="vertical"

  4.               android:background="@drawable/main_background"

  5.               android:id="@+id/mian_bg"

  6.               android:scaleType="fitXY"

  7.               android:gravity="center"

  8.               android:layout_width="fill_parent"

  9.               android:layout_height="fill_parent"

  10.         >

  11.    <ImageView

  12.             android:layout_gravity="center"

  13.             android:src="@drawable/img_main_roll0"

  14.             android:id="@+id/main_cion"

  15.             android:layout_width="180dp"

  16.             android:layout_height="180dp"/>

  17.       

  18. </<LinearLayout>


  19. 先獲取圖片控件:

  20. public ImageView p_w_picpathView;

  21. public LinearLayout linearLayout;


  22. p_w_picpathView=(ImageView)findViewById(R.id.main_cion);

  23. linearLayout=(LinearLayout)findViewById(R.id.mian_bg);

  24. 應在次Activity銷毀時釋放

  25. protected void onDestroy() {

  26.         super.onDestroy();

  27.         p_w_picpathView.setImageBitmap(null);//釋放

  28.         linearLayout.setBackground(null);

  29.         System.gc();//通知進行回收

  30.     }


  31. 使用Bitmap記得不用時調用回收

  32. bitmap.recycle();



  33. 總結:

  34. 無論你是在xml中布局使用了:


  35. android:background   ,


  36. 還是在java代碼中調用了:


  37. setBackground( background );-------API16+


  38. setBackgroundDrawable( background)--------API16-


  39. setBackgroundResource( resid)


  40. 的方式去設置了背景圖片.


  41. 使用的時候,請調用一下對應的方法: 

  42. setBackgroundResource和 android:background → setBackgroundResource(0);


  43. setBackgroundDrawable( background) → setBackgroundDrawable (null)


  44. setBackground ( background ) → setBackground ( null )  

  45. 然后再onDestory中調用System.gc();


復制代碼

2.確定不用的List,數組等參數

釋放:Obj=null即可,list先clear(),在令其等于null;如內存緊張,可及時調用Syetem.gc()通知進行回收


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女