安卓开发中应注意内存的释放,一旦加载图片或其他占用太多内存,此时就会发生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. </<LinearLayout>


  18. 先获取图片控件:

  19. public ImageView imageView;

  20. public LinearLayout linearLayout;


  21. imageView=(ImageView)findViewById(R.id.main_cion);

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

  23. 应在次Activity销毁时释放

  24. protected void onDestroy() {

  25. super.onDestroy();

  26. imageView.setImageBitmap(null);//释放

  27. linearLayout.setBackground(null);

  28. System.gc();//通知进行回收

  29. }


  30. 使用Bitmap记得不用时调用回收

  31. bitmap.recycle();



  32. 总结:

  33. 无论你是在xml中布局使用了:


  34. android:background ,


  35. 还是在java代码中调用了:


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


  37. setBackgroundDrawable( background)--------API16-


  38. setBackgroundResource( resid)


  39. 的方式去设置了背景图片.


  40. 使用的时候,请调用一下对应的方法:

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


  42. setBackgroundDrawable( background) → setBackgroundDrawable (null)


  43. setBackground ( background ) → setBackground ( null )

  44. 然后再onDestory中调用System.gc();


复制代码

2.确定不用的List,数组等参数

释放:Obj=null即可,list先clear(),在令其等于null;如内存紧张,可及时调用Syetem.gc()通知进行回收


更多相关文章

  1. Android(安卓)webkit 事件传递流程通道分析
  2. Android(安卓)7.0 Audio :通话中的音频调用接口
  3. Android下实现injectso
  4. 《Android(安卓)JNI》05 在JNI中调用Java的函数
  5. Android(安卓)ADT 默认的模拟器内存调整
  6. Android(安卓)LiveData 使用详解
  7. Android开发者指南(6) —— AIDL
  8. Android(安卓)调用系统地图(Google Map)并显示具体方位
  9. android persistent属性研究

随机推荐

  1. 802.11(wifi)中SSID、BSSID、ESSID的区别
  2. com.android.cts.devicepolicy.ManagedPr
  3. android 扫描sdcard下的图片
  4. RF Analyzer for Android(安卓)安卓平台
  5. android 拍照和上传
  6. List.addAll(null);
  7. smali文件语法参考
  8. Android(安卓)SQLite数据库增删改查
  9. android 混淆代码gson数据nullpointerexc
  10. GestureDetector使用实例