android out of memory

1.大量查询数据库时cursor没有关闭

错误写法:

 Cursor cursor = getContentResolver().query( );

 if(cursor != null)

{

       cursor.moveTOFirst();

       while(!cursor.isAfterLast())

       {

             ..............

         }

}

正确写法:

Cursor cursor = getContentResolver().query( );

 if(cursor != null)

{

       cursor.moveTOFirst();

       while(!cursor.isAfterLast())

       {

             ..............

         }

 

        cursor.close;

        cursor = null;

}

2. Bitmap对象没有及时回收

   因为Bitmap对象比较占内存,所以,Bitmap对象用完之后,最好使用Bitmap.recyle() 来回收Bitmap对象所占的内存。

3. 在Adapter中没有使用缓存中的convertView

 错误写法:

pubic View getView(int position, View convertView, ViewGroup parent)

{

       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       View view = inflater.inflate(R.layout.listview_item_manage_bookshelves,null);

       ........................

}

正确写法:

pubic View getView(int position, View convertView, ViewGroup parent)

{

      View view;

       if(convertView == null)

      {

              LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

              view = inflater.inflate(R.layout.listview_item_manage_bookshelves,null);

       }

       else

               view  = convertView;

        ........................

}

4. 根据Activity的生命周期,回收资源

   在OnStop() 或者 onDestroy()方法中,对一些方法,对象回收,例如:

   if(mArraryList != null)

         mArrayList = null;

     ......................

    .......................

   System.gc();
   System.gc();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值