Avoiding multiple Garbage Collection execution
Read in some blog that GC in Android happens on main(UI) thread, this may create sluggishness in UI screen depending on the frequency of GC execution. 开发者_如何学PythonHence I was wondering will it be a good idea if I manually release objects(by assigning null value) which has no further use for me. This way we may avoid multiple execution of GC in the application.
Please share your thoughts. Thanks, sku
There's no such thing as "manually releasing objects" -- at least not in any way that's meaningful to GC. An object doesn't immediately get freed/collected/whatever when you lose all references to it; it just becomes eligible for collection. GC is what actually does the releasing of the object, and it does so when it feels like doing so.
The only real way to keep the GC from working so hard is to create fewer objects, particularly temporary objects. Less garbage == less collection.
Releasing (dereferencing) objects for which you have no further use is always a good idea. You can also use SoftReference
, WeakReference
and/or WeakHashMap
to help the GC pick up stuff that you don't mind going away if the system needs space.
There's more information about Android's GC system here.
精彩评论