Herbert- garbage collection can indeed run automatically even if there has been no out of memory error and no explicit gc() call. It isn't guaranteed anywhere I'm aware of, but most implementations of
Java will create a low-priority thread to call System.gc() whenever no higher-priority threads are taking up system time. Note that this does
not mean that
all garbage collection is low-priority. In the case of an impending out-of-memory error, I expect it's pretty high priority - the whole system is about to crash after all if memory isn't freed, so in this situation the JVM had better give GC full and immediate attention. And when you call System.gc(), garbage collection should have the same priority as the thread you called it from, or higher. (I.e. once GC does successfully start it may well hold up all other activity in the JVM until it's done.)
The reason for System.gc() is so you can, err,
encourage the JVM to run at a time that's convenient for you, rather than either (a) when it gets around to it, or (b) when it absolutely has to. Because the latter especially may impact performance.