Is there any way to stop the garbage collector for some time?
A quick workaround is to disable the System. gc () and force a Full GC. The –XX:+DisableExplicitGC command can be passed as a java parameter that disables System. gc() for the JVM.
Garbage collection A managed memory environment, like the ART or Dalvik virtual machine, keeps track of each memory allocation. Once it determines that a piece of memory is no longer being used by the program, it frees it back to the heap, without any intervention from the programmer.
If your app creates a lot of objects, then the Android run time (ART) environment will trigger garbage collection (GC) frequently. Android garbage collection is an automatic process which removes unused objects from memory. However, frequent garbage collection consumes a lot of CPU, and it will also pause the app.
The garbage collector provides the following benefits: Frees developers from having to manually release memory. Allocates objects on the managed heap efficiently. Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations.
The best way of not be lagged by the GC is to avoid the need to collect garbage all the time. You could for example reuse objects instead of nulling them and creating new ones. This quite same pattern as androids CursorAdapter is doing when reusing the View's, it just reused a view to represent new data. The drawback of this that you have to do it manually and program in a very precise way. Also you'll get a better memory handling and a performance boost.
I guess you are willing to avoid getting lagged by the gc. If this is the case, your question is similar to this one.
Basically you can't stop the gc but you can force a call using System.gc()
.
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