Calling System.gc() requests that garbage collection takes place, and isn’t guaranteed. The part about it not being guaranteed is interesting to me: can someone explain the exact process that takes place when you make this call?
Calling System.gc() requests that garbage collection takes place, and isn’t guaranteed. The part about
Share
There are no guarantees that a garbage collection will take place, because there are no guarantees that the JVM supports garbage collection (I don’t believe it’s mentioned in the JLS at all, certainly not in the index).
However, I think this belief that “it’s not guaranteed” is a little misplaced. From the Sun API docs:
OK, so maybe it’s not guaranteed, but this indicates to me that a normal JVM will actually start a GC cycle when you call this method. Perhaps some JVMs won’t, and perhaps some future garbage collector won’t, but for now, it does what’s advertised.
Of course, that said, there’s almost never a reason for application code to call this method. The JVM will collect the garbage when it needs to do so.