问题描述
This回答告诉我,调用 循环()
一个TypedArray的方法允许它被垃圾收集。我的问题是,为什么TypedArray特别需要一种方法,它是垃圾回收?为什么不能只是等待被垃圾就像一个普通的对象收集的?
This answer tells me that calling the recycle()
method of a TypedArray allows for it to be garbage collected. My question is why TypedArray specifically needs a method for it to be garbage collected? Why can't it just wait to be garbage collected like a regular object?
推荐答案
这是必需的缓存purporse。当你调用循环
这意味着这个对象从这点可以被重复使用。内部 TypedArray
包含几个数组所以为了不每次都分配内存时, TypedArray
用于其缓存在资源
类的静态字段。你可以看一下 TypedArray.recycle()
方法code:
This is required for caching purporse. When you call recycle
it means that this object can be reused from this point. Internally TypedArray
contains few arrays so in order not to allocate memory each time when TypedArray
is used it is cached in Resources
class as static field. You can look at TypedArray.recycle()
method code:
/**
* 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;
}
}
}
所以,当你调用循环
的 TypedArray
对象是刚返回到缓存中。
So when you call recycle
your TypedArray
object is just returned back to cache.
这篇关于为什么要TypedArray被回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!