この答えrecycle()
は、 TypedArrayのメソッドを呼び出すと、ガベージコレクションが可能になることを示しています。私の質問は、TypedArray がガベージ コレクションされるメソッドを具体的に必要とするのはなぜですか? 通常のオブジェクトのようにガベージ コレクションされるのを待つことができないのはなぜですか?
7900 次
2 に答える
9
これは、キャッシングの目的で必要です。呼び出す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;
}
}
}
したがってrecycle
、TypedArray
オブジェクトを呼び出すと、キャッシュに返されます。
于 2012-12-10T17:06:34.593 に答える
4
@Andrei Mankevich Android SDK の最新バージョンを確認したところ、recycle() にいくつかの変更が加えられたようです。以下のコードを確認してください。
/**
* Recycle the TypedArray, to be re-used by a later caller. After calling
* this function you must not ever touch the typed array again.
*/
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);
}
于 2015-07-14T19:32:03.567 に答える