2

I wonder should not be use System.gc() in android.

I searched the developer document:

Result:

public static void gc ()

Added in API level 1 Indicates to the VM that it would be a good time to run the garbage collector. Note that this is a hint only. There is no guarantee that the garbage collector will actually be run.

and, find reference infomation. this url : http://code.google.com/p/android/issues/detail?id=8488#c80

but this is so long ago.

if use Bitmap, use bitmap.recycle(). but complex views, how to gc?

Now, is that problem not resolved? and never used System.gc()?

Samee dhoe
  • 19
  • 6
ShakeJ
  • 633
  • 5
  • 10
  • 6
    Your code should work fine without `System.gc()`. If you think you have to use it, change your code, – tolgap Oct 14 '13 at 13:23
  • As said you should not have to call `System.gc()`, it will be called when the system needs to free up memory and if you want you could null some of your unsused allocations that might free up memory. – kabuto178 Oct 14 '13 at 13:26
  • Why would you want to GC a complex view? Unless you're getting actually OOM exceptions or anything like that, you should never ever interfere with the OS and its garbage collection and even with OOM exceptions you can probably work around this issue without doing your own garbage collection... – Darwind Oct 14 '13 at 13:26

1 Answers1

4

Invoking System.gc() is not a guarantee of garbage collection actually being performed. It's more of a hint than anything else.

In Java programming in general it has been regarded as very bad practice to use this method call because using it implies some level of expectation that garbage collection then happens. In truth, when garbage collection occurs depends on many things and is best left to the container and run-time tuning (if required).

I'd say don't use System.gc() in Android for all the same reasons.

Phil Haigh
  • 4,522
  • 1
  • 25
  • 29
  • 1
    Still, pressing the back button in the toolbar seems to automatically invoke `System.gc()` since it frees memory while the real back button doesn't, even after I call `bitmap.recycle()` and `finish()` when it's pressed. Only `System.gc()` actually frees memory in my device. – Dielson Sales Oct 10 '15 at 23:21