android内存溢出的解决方法

1.当项目中包含大量的图片的时候或者图片的数量太大的时候:

BitmapFactory.Options options = new BitmapFactory.Options();  
options.inSampleSize = 4

2.对图片采用软引用,及时的的recyle() 操作


SoftReference<Bitmap> bitmap;  
bitmap = new SoftReference<Bitmap>(pBitmap);  
if(bitmap != null){  
if(bitmap.get() != null && !bitmap.get().isRecycled()){
      bitmap.get().recycle();  
    bitmap = null;
    }  
   }

3.对复杂的ListView进行合理的设计与编码,注意Adapter的convertView的复用,以及holder机制的运用

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {  
            v = mInflater.inflate(resource, parent, false);
         final int[] to = mTo;  
        final int count = to.length;
       final View[] holder = new View[count];  
           for (int i = 0; i < count; i++) {  
             holder[i] = v.findViewById(to[i]);
    }  
         v.setTag(holder);       } else {  
   }  
}

4.单个页面横竖屏切换多次之后OOM

    4.1看布局图片中有没有大的图片,比如背景图片之类的东西,取出Xml中的相关设置,改在onCreate中手动Java代码设置背景图片

在Activity中的onDestory()中设置当前北京图片的bg.setCallback(null),防止Activity的背景图片得不到相应的释放

    4.2在页面切换的时候,尽量减少对数据库的操作,反复的使用某个对象

5.Android堆内存可以自定义大小和优化Dalvik 虚拟机的内存

private final static int CWJ_HEAP_SIZE= 6*1024*1024;  
private final static float TARGET_HEAP_UTILIZATION = 0.75f;   
VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);  
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION)

转载地址:http://bbs.51cto.com/thread-955196-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值