开发者

Cleaning up StateListDrawables

开发者 https://www.devze.com 2023-03-11 03:50 出处:网络
I am creating an android application that dynamically downloads images from the internet and then places them onto an ImageView, allowing the user to set a default image and a high开发者_如何学编程lig

I am creating an android application that dynamically downloads images from the internet and then places them onto an ImageView, allowing the user to set a default image and a high开发者_如何学编程lighted image. To do this I download the data and save it to a file, from there whenever the image is needed I create a BitmapDrawable from the file and place it in a StateListDrawable. When the ImageView is no longer needed what is the correct way to dispose of the StateListDrawable so I don't leak memory and they get garbage collected? Should I hold references to each BitmapDrawable in the StateListDrawable so I can call recycle on them? What about setting the StateListDrawable callbacks to null? Or is setting the ImageView to null enough to not leak memory?

Thanks!


The Java VM will automatically take care of collecting your unwanted StateListDrawables. All you have to do is make sure there is no way the StateListDrawable itself can be referenced from your program, either by reassigning the references to another Object, or by setting the references to null. It does not even matter if the StateListDrawable still holds references to other objects you DO want to keep (such as the callbacks you discussed)...once an Object cannot be referenced, it is eligible for garbage collection. When the VM deems it a good time to free up memory, it will take care of deleting the StateListDrawables for you, you do not need to worry about calling recycle() on any of your objects yourself.

A simple way to see this if you are editing using Eclipse is to view the LogCat messages while your program is running in the emulator. There will occasionally be messages saying: "GC freed xxx objects / xxx bytes in xxx ms."

From the Java tutorials:

"Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed. The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection."

Edit: From the documentation for 'Bitmap.recycle()`,

"Free the native object associated with this bitmap, and clear the reference to the pixel data...This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap."

0

精彩评论

暂无评论...
验证码 换一张
取 消