android内存优化

android内存优化总结  
   在写app的时候,越是到后面越是要考虑到app实际效率和体验的问题。往往在写个人的项目时,这方面考虑的比较少,因为用户少,而且有些问题可能不是很复杂。但是在做公司的项目时就必须考虑周到。比如内存优化,recyclerview优化,界面优化等。避免出现oom、anr等错误。
         为什么需要内存优化     现在的手机配置都不一样,在开发app的时候应该考虑到大多数手机的实际情况,所以一般app占用内存不要超过50m。否则会出现app相当卡顿的样子。也可能出现app的ANR错误。这是一个优秀app开发必然要考虑的问题。
        内存优化的方式
           图片:    在很多内存崩溃的情况中最多的原因就是图片过多过大。那么针对图片进行一些优化是必要的。   

 (1)、使用option进行压缩。

public static Bitmap createImageThumbnail(String filePath){  
     Bitmap bitmap = null;  
     BitmapFactory.Options opts = new BitmapFactory.Options();  
     opts.inJustDecodeBounds = true;  
     BitmapFactory.decodeFile(filePath, opts);  
  
     opts.inSampleSize = computeSampleSize(opts, -1, 128*128);  
     opts.inJustDecodeBounds = false;  
  
     try {  
         bitmap = BitmapFactory.decodeFile(filePath, opts);  
     }catch (Exception e) {  
        // TODO: handle exception  
    }  
    return bitmap;  
}  
  
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {  
    int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);  
    int roundedSize;  
    if (initialSize <= 8) {  
        roundedSize = 1;  
        while (roundedSize < initialSize) {  
            roundedSize <<= 1;  
        }  
    } else {  
        roundedSize = (initialSize + 7) / 8 * 8;  
    }  
    return roundedSize;  
}  
  
private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {  
    double w = options.outWidth;  
    double h = options.outHeight;  
    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));  
    int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));  
    if (upperBound < lowerBound) {  
        // return the larger one when there is no overlapping zone.  
        return lowerBound;  
    }  
    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {  
        return 1;  
    } else if (minSideLength == -1) {  
        return lowerBound;  
    } else {  
        return upperBound;  
    }  
}  
		


 
(2)、使用LRUcache      

    LRU指的是回收最近不是经常使用的资源。LRUcache使用的是key、value的形式,对于图片。实现:   

     1、获取图片所需的缓存空间。(重写sizeof方法)    

private LruCache<String, Bitmap> mMemoryCache;
private LruCacheUtils() {
        if (mMemoryCache == null)
            mMemoryCache = new LruCache<String, Bitmap>(
                    MAXMEMONRY / 8) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    // 重写此方法来衡量每张图片的大小,默认返回图片数量。
                    return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
                }

                @Override
                protected void entryRemoved(boolean evicted, String key,
                        Bitmap oldValue, Bitmap newValue) {
                    Log.v("tag", "hard cache is full , push to soft cache");
                   
                }
            };
    }

2、实现图片操作


public void clearCache() {
        if (mMemoryCache != null) {
            if (mMemoryCache.size() > 0) {
                Log.d("CacheUtils",
                        "mMemoryCache.size() " + mMemoryCache.size());
                mMemoryCache.evictAll();
                Log.d("CacheUtils", "mMemoryCache.size()" + mMemoryCache.size());
            }
            mMemoryCache = null;
        }
    }

    public synchronized void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (mMemoryCache.get(key) == null) {
            if (key != null && bitmap != null)
                mMemoryCache.put(key, bitmap);
        } else
            Log.w(TAG, "the res is aready exits");
    }

    public synchronized Bitmap getBitmapFromMemCache(String key) {
        Bitmap bm = mMemoryCache.get(key);
        if (key != null) {
            return bm;
        }
        return null;
    }

    /**
     * 移除缓存
     * 
     * @param key
     */
    public synchronized void removeImageCache(String key) {
        if (key != null) {
            if (mMemoryCache != null) {
                Bitmap bm = mMemoryCache.remove(key);
                if (bm != null)
                    bm.recycle();
            }
        }
    }

(3)、软引用(已经不推崇使用,因为gc主要处理对象就是软引用和弱引用,现在就用LRUcache)

(4)、选择RGB_565 (一个像素两个byte)  代替 ALPHA_8(1)、ARGB_444(2)、ARGB_8888(4)

(5)、使用完图片要及时recycle

二、变量    1、尽量使用final static代替static 

   2、能直接访问的字段就不要使用get、set了。  

  3、全局变量尽量少一些。 

   4、能放在外面的东西尽量不要放在for循环当中。   

 以上就是一些基础的吧,希望对大家有所帮助。

本研究利用Sen+MK方法分析了特定区域内的ET(蒸散发)趋势,重点评估了使用遥感数据的ET空间变化。该方法结合了Sen斜率估算器和Mann-Kendall(MK)检验,为评估长期趋势提供了稳健的框架,同时考虑了时间变化和统计显著性。 主要过程与结果: 1.ET趋势可视化:研究利用ET数据,通过ET-MK和ET趋势图展示了蒸散发在不同区域的空间和时间变化。这些图通过颜色渐变表示不同的ET水平及其趋势。 2.Mann-Kendall检验:应用MK检验来评估ET趋势的统计显著性。检验结果以二元分类图呈现,标明ET变化的显著性,帮助识别出有显著变化的区域。 3.重分类结果:通过重分类处理,将区域根据ET变化的显著性进行分类,从而聚焦于具有显著变化的区域。这一过程确保分析集中在具有实际意义的发现上。 4.最终输出:最终结果以栅格图和png图的形式呈现,支持各种应用,包括政策规划、水资源管理和土地利用变化分析,这些都是基于详细的时空分析。 ------------------------------------------------------------------- 文件夹构造: data文件夹:原始数据,支持分析的基础数据(MOD16A2H ET数据 宁夏部分)。 results文件夹:分析结果与可视化,展示研究成果。 Sen+MK_optimized.py:主分析脚本,适合批量数据处理和自动化分析。 Sen+MK.ipynb:Jupyter Notebook,复现可视化地图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值