零、应该掌握的
- TypedArray 的 基本用法;
- TypedArray 对象是如何生成的?
- TypedArray 与单例模式
- SynchronizedPool 同步对象池、SimplePool 简单对象池、Pool 接口
- equals 与 == 的区别
一、思考Why:
在 Android 自定义 View 的时候,需要使用 TypedArray 来获取 XML layout 中的属性值,使用完之后,需要调用 recyle() 方法将 TypedArray 回收。
那么问题来了,这个TypedArray是个什么东西?为什么需要回收呢?TypedArray并没有占用IO,线程,它仅仅是一个变量而已,为什么需要 recycle?
为了解开这个谜,首先去找官网的 Documentation,到找 TypedArray 方法,得到下面一个简短的回答:
告诉我们在确定使用完之后调用 recycle() 方法。于是进一步查看该方法的解释,如下:
简单翻译下来,就是说:回收 TypedArray,用于后续调用时可复用之。当调用该方法后,不能再操作该变量。
同样是一个简洁的答复,但没有解开我们心中的疑惑,这个TypedArray背后,到底隐藏着怎样的秘密……
求之不得,辗转反侧,于是我们决定深入源码,一探其究竟……
二、TypedArray 对象的生成过程
首先,是 TypedArray 的常规使用方法:
TypedArray array = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.PieChart,0,0);
try {
mShowText = array.getBoolean(R.styleable.PieChart_showText,false);
mTextPos = array.getInteger(R.styleable.PieChart_labelPosition,0);
}finally {
array.recycle();
}
可见,TypedArray不是我们new出来的,而是调用了 obtainStyledAttributes 方法得到的对象,该方法实现如下:
public TypedArray obtainStyledAttributes(AttributeSet set,
int[] attrs, int defStyleAttr, int defStyleRes) {
final int len = attrs.length;
final TypedArray array = TypedArray.obtain(Resources.this, len);
// other code .....
return array;
}
我们只关注当前待解决的问题,其他的代码忽略不看。从上面的代码片段得知,TypedArray也不是它实例化的,而是调用了TypedArray的一个静态方法,得到一个实例,再做一些处理,最后返回这个实例。看到这里,我们似乎知道了什么,,,带着猜测,我们进一步查看该静态方法的内部实现:
/**
* Container for an array of values that were retrieved with
* {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}
* or {@link Resources#obtainAttributes}. Be
* sure to call {@link #recycle} when done with them.
*
* The indices used to retrieve values from this structure correspond to
* the positions of the attributes given to obtainStyledAttributes.
*/
public class TypedArray {
static TypedArray obtain(Resources res, int len) {
final TypedArray attrs = res.mTypedArrayPool.acquire();
if (attrs != null) {
attrs.mLength = len;
attrs.mRecycled = false;
final int fullLen = len * AssetManager.STYLE_NUM_ENTRIES;
if (attrs.mData.length >= fullLen) {
return attrs;
}
attrs.mData = new int[fullLen];
attrs.mIndices = new int[1 + len];
return attrs;
}
return new TypedArray(res,
new int[len*AssetManager.STYLE_NUM_ENTRIES],
new int[1+len], len);
}
// Other members ......
}
仔细看一下这个方法的实现,我想大部分人都明了了,该类没有公共的构造函数,只提供静态方法获取实例,显然是一个典型的单例模式。在代码片段的第 13 行,很清晰的表达了这个 array 是从一个 array pool的池中获取的。
因此,我们得出结论:
程序在运行时维护了一个 TypedArray的池,程序调用时,会向该池中请求一个实例,用完之后,调用 recycle() 方法来释放该实例,从而使其可被其他模块复用。
那为什么要使用这种模式呢?答案也很简单,TypedArray的使用场景之一,就是上述的自定义View,会随着 Activity的每一次Create而Create,因此,需要系统频繁的创建array,对内存和性能是一个不小的开销,如果不使用池模式,每次都让GC来回收,很可能就会造成OutOfMemory。
这就是使用池+单例模式的原因,这也就是为什么官方文档一再的强调:使用完之后一定 recycle,recycle,recycle。
三、mTypedArrayPool 源码简单分析
当在自定义View 中 调用:
mTypedArray.recycle(); //回收
紧接着
public void recycle() {
if (mRecycled) {
throw new RuntimeException(toString() + " recycled twice!");
}
mRecycled = true;
// These may have been set by the client.
mXml = null;
mTheme = null;
mResources.mTypedArrayPool.release(this);
}
这个TypedArray 的公共方法有两个细节:
(1)、主题资源置空
mXml = null;
mTheme = null;
// XmlBlock.Parser mXml;
// Resources.Theme mTheme;
(2)、调用 mResources.mTypedArrayPool.release(this) 方法
我们先看看 mTypedArrayPool 是什么?
/**
* Synchronized) pool of objects.
* 同步对象池
* @param <T> The pooled type.
*/
public static class SynchronizedPool<T> extends SimplePool<T> {
private final Object mLock = new Object(); //锁对象
/**
* Creates a new instance.
*
* @param maxPoolSize The max pool size.
*
* @throws IllegalArgumentException If the max pool size is less than zero.
*/
public SynchronizedPool(int maxPoolSize) {
super(maxPoolSize);
}
@Override
public T acquire() {
synchronized (mLock) {
return super.acquire(); // 取出一个对象
}
}
@Override
public boolean release(T element) {
synchronized (mLock) {
return super.release(element); // 释放一个对象
}
}
}
可以看到:
- SynchronizedPool 是 android.support.v4.util包下的 Pools 类的内部静态类;
- SynchronizedPool 是 SimplePool 的子类;
- SimplePool 实现了 Pool 接口,并实现了 acquire() 方法和release(T instance) 方法;
同时可以从类 SimplePool 的构造方法中看出,mPool是一个Object 数组:
/**
* Creates a new instance.
*
* @param maxPoolSize The max pool size.
*
* @throws IllegalArgumentException If the max pool size is less than zero.
*/
public SimplePool(int maxPoolSize) {
if (maxPoolSize <= 0) {
throw new IllegalArgumentException("The max pool size must be > 0");
}
mPool = new Object[maxPoolSize];
}
再回到 mTypedArrayPool 初始化的地方:
final SynchronizedPool<TypedArray> mTypedArrayPool = new SynchronizedPool<>(5);
就知道 mTypedArrayPool 其实就是一个长度为5 的 对象数组,当调用 TypedArray.obtain()方法的时候,就调用 final TypedArray attrs = res.mTypedArrayPool.acquire(); 从 mTypedArrayPool中取出一个 TypedArray,然后判断取出 的attrs 是不是 null 。如果不为null , 就返回此对象, 如果取出的是 null ,则 执行:
return new TypedArray(res,
new int[len*AssetManager.STYLE_NUM_ENTRIES],
new int[1+len], len);
可以看到,应用最开始提供含5 个TypedArray 对象的mTypedArrayPool 供用户使用,只有当这个5 个TypedArray 对象都被占用(mTypedArrayPool 为空)的时候,才会new 一个对象。
四、mTypedArrayPool 的取对象和释放对象
取对象,调用SimplePool 的acquire( )方法
@Override
@SuppressWarnings("unchecked")
public T acquire() {
if (mPoolSize > 0) {
final int lastPooledIndex = mPoolSize - 1;
T instance = (T) mPool[lastPooledIndex]; //在数组中,从后往前取
mPool[lastPooledIndex] = null; // 取完后,最后一个对象置 null
mPoolSize--; // 对象池的长度 减一
return instance;
}
return null;
}
释放对象调用 release(T instance) 方法:
@Override
public boolean release(T instance) {
if (isInPool(instance)) { // 对于引用类型变量,先通过 == 来判断内存地址是否相同,进而判断该对象是否已经在对象池中,如果已经存在则抛出异常;
throw new IllegalStateException("Already in the pool!");
}
if (mPoolSize < mPool.length) {
mPool[mPoolSize] = instance; // 将数组最后一个为null 的对象赋值为instance;
mPoolSize++; // 长度加一
return true;
}
return false;
}
调用 isInPool(T instance) 判断instance 是否已经存在对象池中:
private boolean isInPool(T instance) {
for (int i = 0; i < mPoolSize; i++) {
if (mPool[i] == instance) { //== 是比较对象的地址是否相等
return true;
}
}
return false;
}
参考致谢:
(1)、官方API
(2)、关于对象池的一些分析
(3)、Object Pool