Should we use Runtime.gc() or System.gc() for clearing memory (manual garbage collection) in production code in onLowMemory() method in Application class?
It's a bad practice, using System.gc() is not meaning you use manually gc it's only hint to jvm to wipe off garbage. Better to not use this method like finalize() in Object, both of them not provide any guarantee. In javadoc of Application said that system will perform gc after returning from this method and given way to do it in the right manner.
You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.
Application javadoc
So when you use System.gc() in onLowMemory() method after return from this method, will be another garbage collection effort. From hint in javadoc it's better to lost link to caches in example List<Object> bigCache = null;  after method end will be garbage collection that pick up that cache list and memory will free.
No. From the Javadoc:
You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With