Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of recycle() method in TypedArray

Tags:

android

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?

like image 202
Pramod Avatar asked Aug 31 '11 05:08

Pramod


People also ask

What is Typedarray recycle?

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 .

Why it is important to use the recycle () method on every Java object?

The recycle method unconditionally destroys an object and returns its memory to the system. In the latter method, the vector contains the Domino® Objects to be recycled. This method effectively batches recycle operations and is especially efficient for remote (IIOP) calls.

What is typed array in Android?

Container for an array of values that were retrieved with Resources. Theme#obtainStyledAttributes(AttributeSet, int[], int, int) or Resources#obtainAttributes .


2 Answers

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.

like image 52
Vinay Avatar answered Oct 15 '22 05:10

Vinay


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.

like image 23
Ronnie Avatar answered Oct 15 '22 07:10

Ronnie