Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i stop the android garbage collector? Is it possible?

Is there any way to stop the garbage collector for some time?

like image 618
Simon Avatar asked May 21 '10 11:05

Simon


People also ask

Can you disable garbage collection?

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.

What is garbage collector Android?

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.

Does Android use garbage collection?

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.

What is the reason behind garbage collector?

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.


2 Answers

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.

like image 182
Moss Avatar answered Oct 05 '22 11:10

Moss


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().

like image 26
Macarse Avatar answered Oct 05 '22 10:10

Macarse