views:

129

answers:

3

I've seem many Android answers that suggest calling the garbage collector in some situations.

Is it a good practice to request the garbage collector in Android before doing a memory-hungry operation? If not, should I only call it if I get an OutOfMemory error?

Are there other things I should use before resorting to the garbage collector?

+3  A: 

If you get an OutOfMemoryError then it's usually too late to call the garbage collector...

Here is quote from Anfroid Developer:

Most of the time, garbage collection occurs because of tons of small, short-lived objects and some garbage collectors, like generational garbage collectors, can optimize the collection of these objects so that the application does not get interrupted too often. The Android garbage collector is unfortunately not able to perform such optimizations and the creation of short-lived objects in performance critical code paths is thus very costly for your application.

So to my understanding, there is no urgent need to call the gc. It's better to spend more effort in avoiding the unnecessary creation of objects (like creation of objects inside loops)

Andreas_D
Can't the quote be interpreted the other way? Maybe the GC needs to be called manually in order to collect those objects before they consume too much memory.
hgpc
@hgpc: I don't see how the quote can be interpreted in the way you suggest. I'm guessing the documentation is a confession, admitting that their GC is simple; when memory runs low, a full GC is executed.
GregS
A: 

There is no need to call the garbage collector after an OutOfMemoryError.

It's Javadoc clearly states:

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

So, the garbage collector already tried to free up memory before generating the error but was unsuccessful.

perdian
The Android Javadoc does not state that, and most likely its implementation is different. http://d.android.com/reference/java/lang/OutOfMemoryError.html
hgpc
+1  A: 

Generally speaking, in the presence of a garbage collector, it is never good practice to manually call the GC. A GC is organized around heuristic algorithms which work best when left to their own devices. Calling the GC manually often decreases performance.

Occasionally, in some relatively rare situations, one may find that a particular GC gets it wrong, and a manual call to the GC may then improves things, performance-wise. This is because it is not really possible to implement a "perfect" GC which will manage memory optimally in all cases. Such situations are hard to predict and depend on many subtle implementation details. The "good practice" is to let the GC run by itself; a manual call to the GC is the exception, which should be envisioned only after an actual performance issue has been duly witnessed.

Thomas Pornin
+1 for a good platform-independent answer. Waiting to see if someone comes up with an Android-specific answer before choosing.
hgpc
@Thomas Pornin: I agree with what you wrote *if* you allow "performance" to mean something more general than CPU efficiency. On an Android device, the user's perception of performance is paramount. The developer may be prefer to run the GC while the user is pressing buttons for example, so the user will not be aware of the GC.
GregS