50

I have created a GalleryView and ImageView which displays the Image bigger when an item is clicked in the gallery. I have used the below code to implement the ImageAdapter:

public ImageAdapter(Context c)
{
    context = c;
    TypedArray a = obtainStyledAttributes(R.styleable.gallery1);
    itemBackground = a.getResourceId(R.styleable.gallery1_android_galleryItemBackground, 0);    
    a.recycle();    
}

When I removed the statement a.recycle() there is no change and the app is running normally as before, but everywhere I read that it is compulsory to recycle the typedArray. When there is no change in the way my app is running what is the use of the recycle() method?

AAEM
  • 1,837
  • 2
  • 18
  • 26
Pramod
  • 1,411
  • 11
  • 35
  • 66

4 Answers4

52

The point is similar to the idea of clearing a pointer in a C-language (if you're familiar with that). It is used to make the data associated with a ready for garbage collection so memory/data is not inefficiently bound to a when it doesn't need to be. Read more here.

It's important to note that this isn't really necessary unless you're actually reusing "a". GC should automatically clear up this data for you if the object is not used again. The reason why a TypedArray is different, however, is because a TypedArray has other internal data that must be returned (known as StyledAttributes) to the TypedArray for later reuse. Read about that here.

AAEM
  • 1,837
  • 2
  • 18
  • 26
Vinay
  • 6,204
  • 6
  • 38
  • 55
  • 2
    Side Note: GC stands for "garbage collection". – Vinay Aug 31 '11 at 05:34
  • 3
    I don't understand. The last link makes it seem like calling recycle() allows some internal array to be reused. Doesn't this mean that it prevents the array from being GCed? Why do you say that it makes the data associated with "a" ready for GC? – gsgx May 23 '13 at 03:58
  • 1
    @gsingh2011 it allows it to "be re-used by a later caller". Admittedly the documentation wording is a bit awkward, but what they're getting at is that the memory associated with the `TypedArray` can be re-used by a later caller (not the instance itself as you understand it within the confines of your program). That's why the documentation also says "After calling this function you must not ever touch the typed array again.". – Vinay Feb 24 '15 at 08:44
12

The recycle() causes the allocated memory to be returned to the available pool immediately and will not stay until garbage collection. This method is also available for Bitmap.

Ron
  • 24,175
  • 8
  • 56
  • 97
0

recycle basically means..free/clearing all the data associated with corresponding resource. In Android we can find recycle for Bitmap and TypedArray.

If you check both source files then you can find a boolean variable "mRecycled" which is "false"(default value). It is assigned to "true" when recycle is called.

So, Now if you check that method(recycle method in both the classes) then you we can observe that they are clearing all the values.

For reference here are the methods.

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);
}

this line

mResources.mTypedArrayPool.release(this);

will release the typedArray from the SunchronisedPool whose default value is 5. So you shouldn't use same typedArray again as it gets cleared.

once "mRecycled" of TypedArray is true then while getting its properties it will throw RuntimeException saying "Cannot make calls to a recycled instance!".

simliar behaviour incase of Bitmap as well. Hope it helps.

GvSharma
  • 2,632
  • 1
  • 24
  • 30
  • Can someone describe the design purpose of recycling here? I generally think of "pools" as a way to conserve resources (e.g. max memory footprint or processing time) and let processes wait rather than creating new objects on demand, where peak demand may be very large or initializing new objects is expensive. The explanations of the TypedArray here seem to indicate there is plenty of garbage collection going on when recycling anyway, so is it that costly to recycle the whole array? I'm trying to get a better feel for the recycling pattern. – sb4 Jul 20 '20 at 20:49
0

As its use is over[after initializing our local attributes] So we recycle it back to the Resource pool

Simple