添加自定义字体引起的Memory Leak

问题描述

在项目中引入了新的字库,项目中也自定了一个TextView 和 EditView 使用这个新的字库。代码如下:

public class CustomCashDigitFontTextView extends android.support.v7.widget.AppCompatTextView {

    public CustomCashDigitFontTextView(Context context) {
        super(context);
        init(context);
    }

    public CustomCashDigitFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomCashDigitFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void init(Context context) {
        Typeface newFont = Typeface.createTypeface(context.getAssets(), "DIN-Medium-Number.ttf");
        setTypeface(newFont);
    }
}

我用下面的命令查看memory状况的时候发现:
adb shell dumpsys meminfo
内存的状况如下:

Asset Allocations
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
    zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K

asserts部分同样的字体库,内存被分配了多次。

原因分析

主要是下面的代码引起的:

Typeface newFont = Typeface.createFromAsset(context.getAssets(), "DIN-Medium-Number.ttf");

每一次调用Typeface.createFromAsset,都会分配一次内存,导致同样的内容内存反复分配。
我有观察了最新的Android 源码,在最新的Android7.0的代码中,这个问题已经解决了。但是在Android 5.0+的版本上,问题依然存在。

解决方案

方案很简单,引入一个缓存,当时同样的字体,不再分配新的内存。

public class TypefaceHelper {
    private static final LruCache<String, Typeface> sDynamicTypefaceCache = new LruCache<>(8);

    public static Typeface createTypeface(AssetManager assetManager, String path) {
        synchronized (sDynamicTypefaceCache) {
            Typeface typeFace = sDynamicTypefaceCache.get(path);
            if (typeFace!=null) {
                return typeFace;
            } else {
                typeFace = Typeface.createFromAsset(assetManager, path);
                sDynamicTypefaceCache.put(path, typeFace);
                return typeFace;
            }
        }
    }
}

注意,要加上synchronized (sDynamicTypefaceCache) 线程同步锁,否则,在某些线程同步的情况下,也可能导致偶先的多次分配。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值