This answer은 TypedArray의 recycle()
메서드를 호출하면 가비지 수집이 가능하다는 것을 알려줍니다. 내 질문에 왜 TypedArray 특별히 가비지 수집 할 수있는 메서드가 필요합니까? 일반 객체처럼 가비지 수집을 기다릴 수없는 이유는 무엇입니까?TypedArray를 재활용해야하는 이유는 무엇입니까?
9
A
답변
5
이것은 캐싱 제품에 필요합니다. 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
객체는 그냥 캐시로 반환됩니다.
2
@Andrei Mankevich Android SDK의 최신 버전을 확인하면 재활용()으로 변경된 것 같습니다. 아래의 코드를 확인하십시오 :
/**
* 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);
}
/** * 이후의 호출에 의해 다시 사용하는하는 TypedArray을 재활용합니다. *이 함수를 호출 한 후에는 입력 된 배열을 다시 만지면 안됩니다. */ public void recycle() { if (mRecycled) { 새로운 RuntimeException을 throw합니다 (toString() + "recycled twice!"); } mRecycled = true; // 클라이언트에 의해 설정되었을 수 있습니다. mXml = null; mTheme = null; mResources.mTypedArrayPool.release (this); } – Sam003