RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Android的OutOfMemory解决

安卓开发中应注意内存的释放,一旦加载图片或其他占用太多内存,此时就会发生OOM错误,即内存泄露。

成都创新互联的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括成都网站制作、成都做网站、外贸营销网站建设、电商网站开发、微信营销、系统平台开发。


在开发中,尤其应注意图片资源的释放。

1。背景图片和ImageView释放------尤其注意图片资源

如:

  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.    

  10.             android:layout_gravity="center"

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

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

  13.             android:layout_width="180dp"

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

  15.       

  16. 先获取图片控件:

  17. public ImageView p_w_picpathView;

  18. public LinearLayout linearLayout;

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

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

  21. 应在次Activity销毁时释放

  22. protected void onDestroy() {

  23.         super.onDestroy();

  24.         p_w_picpathView.setImageBitmap(null);//释放

  25.         linearLayout.setBackground(null);

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

  27.     }

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

  29. bitmap.recycle();

  30. 总结:

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

  32. android:background   ,

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

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

  35. setBackgroundDrawable( background)--------API16-

  36. setBackgroundResource( resid)

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

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

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

  40. setBackgroundDrawable( background) → setBackgroundDrawable (null)

  41. setBackground ( background ) → setBackground ( null )  

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

复制代码

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

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


当前文章:Android的OutOfMemory解决
标题链接:http://scyingshan.cn/article/jciijh.html