What happened internally (JVM) when System.gc() or finalize() method called?

Tony picture Tony · May 21, 2009 · Viewed 7.1k times · Source

What happened internally (JVM) when System.gc() or finalize() method called?

Is this really collect garbage or reduce performance ?

Answer

Bill the Lizard picture Bill the Lizard · May 21, 2009

Exactly what happens when you call System.gc() is JVM dependent. The JVM looks at this call as a suggestion that it might be a good time to run the garbage collector, so don't depend on it.

An object's finalize() method is run on an object by the garbage collector when the JVM determines that there are no more reachable references to that object. You shouldn't call it in your code. Keep in mind that finalize() is not called at the time when your program loses that final reference, but at a future time when the garbage collector is run, so don't depend on it happening at a specific point in time (or at all).