问题描述

在项目中引入了新的字库,项目中也自定了一个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 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) 线程同步锁,否则,在某些线程同步的情况下,也可能导致偶先的多次分配。

更多相关文章

  1. Android(安卓)内存回收机制:回收Activity,还是杀掉Process?
  2. Android(安卓)关键字 收集
  3. Android(安卓)Property System
  4. Android之获得内存剩余大小与总大小
  5. 谈谈对Linux的Huge Pages与Transparent Huge Pages的认识,以及为
  6. Android中对app应用内存的分配
  7. Android Bitmap内存溢出问题解释

随机推荐

  1. android 中使用sqLite例子
  2. android中 sqlite sql操作
  3. Android必备软件
  4. Android(安卓)小项目之--解析如何获取SDC
  5. Android API开发之OpenGL开发之Android O
  6. Android环境建立之编译Android内核源码笔
  7. Cordova更改Gradle版本
  8. android kitkat(4.4以上)各个版本的特性解
  9. Android 开机自启动 App
  10. 根据文字的多少,自动适应变化的表格...