简体   繁体   中英

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. Hence 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM