为什么TypedArray应该被回收?

gsi*_*011 9 android garbage-collection typedarray

这个答案告诉我,调用recycle()TypedArray 的方法允许它被垃圾收集.我的问题是为什么TypedArray特别需要一个方法来进行垃圾回收?为什么它不能等待像常规对象一样被垃圾收集?

And*_*ich 6

这是缓存purporse所必需的.调用recycle它意味着可以从这一点重用此对象.在内部TypedArray包含很少的数组,所以为了不在每次使用时分配内存,TypedArray它在Resources类中被缓存为静态字段.你可以看一下TypedArray.recycle()方法代码:

/**
 * Give back a previously retrieved StyledAttributes, for later re-use.
 */
public void recycle() {
    synchronized (mResources.mTmpValue) {
        TypedArray cached = mResources.mCachedStyledAttributes;
        if (cached == null || cached.mData.length < mData.length) {
            mXml = null;
            mResources.mCachedStyledAttributes = this;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,当你调用recycle你的TypedArray对象是刚刚返回到高速缓存.


归档时间:

查看次数:

7299 次

最近记录:

10 年,4 月 前