安卓开发中应注意内存的释放,一旦加载图片或其他占用太多内存,此时就会发生OOM错误,即内存泄露。 
 在开发中,尤其应注意图片资源的释放。 1。背景图片和ImageView释放------尤其注意图片资源 如: <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                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"
          >
     <ImageView
              android:layout_gravity="center"
              android:src="@drawable/img_main_roll0"
              android:id="@+id/main_cion"
              android:layout_width="180dp"
              android:layout_height="180dp"/>
        
  </<LinearLayout>
  
 
 先获取图片控件:
  public ImageView p_w_picpathView;
  public LinearLayout linearLayout;
  
 
 p_w_picpathView=(ImageView)findViewById(R.id.main_cion);
  linearLayout=(LinearLayout)findViewById(R.id.mian_bg);
  应在次Activity销毁时释放
  protected void onDestroy() {
          super.onDestroy();
          p_w_picpathView.setImageBitmap(null);//释放
          linearLayout.setBackground(null);
          System.gc();//通知进行回收
      }
  
 
 使用Bitmap记得不用时调用回收
  bitmap.recycle();
  
 
 
 
 总结:
  无论你是在xml中布局使用了:
  
 
 android:background   ,
  
 
 还是在java代码中调用了:
  
 
 setBackground( background );-------API16+
  
 
 setBackgroundDrawable( background)--------API16-
  
 
 setBackgroundResource( resid)
  
 
 的方式去设置了背景图片.
  
 
 使用的时候,请调用一下对应的方法: 
  setBackgroundResource和 android:background → setBackgroundResource(0);
  
 
 setBackgroundDrawable( background) → setBackgroundDrawable (null)
  
 
 setBackground ( background ) → setBackground ( null )  
  然后再onDestory中调用System.gc();
  
 
 
 复制代码 2.确定不用的List,数组等参数 释放:Obj=null即可,list先clear(),在令其等于null;如内存紧张,可及时调用Syetem.gc()通知进行回收  |