回收基本上是指..自由/清除所有與相應資源相關的數據。 在Android中,我們可以找到Bitmap和TypedArray的回收站。
如果你檢查兩個源文件,那麼你可以找到一個布爾變量「mRecycled」,它是「false」(默認值)。它在調用回收時被分配到「true」。
所以,現在如果你檢查該方法(在這兩個類中的回收方法),那麼你可以觀察到他們正在清除所有的值。
這裏有參考的方法。
Bitmap.java:
public void recycle() {
if (!mRecycled && mNativePtr != 0) {
if (nativeRecycle(mNativePtr)) {
// return value indicates whether native pixel object was actually recycled.
// false indicates that it is still in use at the native level and these
// objects should not be collected now. They will be collected later when the
// Bitmap itself is collected.
mBuffer = null;
mNinePatchChunk = null;
}
mRecycled = true;
}
}
TypedArray.java
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;
mAssets = null;
mResources.mTypedArrayPool.release(this);
}
此行
mResources.mTypedArrayPool.release(this);
將其默認值是5 所以你SunchronisedPool釋放typedArray不應該再次使用相同的typedArray,因爲它被清除。
一旦TypedArray的「mRecycled」爲true,那麼在獲取其屬性時,它將拋出RuntimeException,說「無法調用回收實例!」。
同樣的行爲也包含在Bitmap中。 希望它有幫助。
邊注意:GC代表「垃圾回收」。 – Vinay
我不明白。最後一個鏈接看起來像調用recycle()允許一些內部數組被重用。這是不是意味着它阻止了數組被GCed?你爲什麼說它使與「a」相關的數據準備好用於GC? – gsingh2011
@ gsingh2011它允許它「被以後的調用者重新使用」。無可否認,文檔的措辭有些尷尬,但是他們得到的是與TypedArray相關的內存可以被更高的調用者重用(而不是你自己在程序範圍內理解的實例本身)。這就是爲什麼文檔也說「調用這個函數後,你不能再次觸摸類型數組。」。 – Vinay