What happened internally (JVM) when System.gc() or finalize() method called?
Is this really collect garbage or reduce performance ?
What happened internally (JVM) when System.gc() or finalize() method called?
Is this really collect garbage or reduce performance ?
By invoking System.gc() the garbage collector is run (as the name says). At this point when the objects are really being removed the finalize() is called on these objects before they vanish.
The last question shouldn't have an "or" in it. It is garbage collection and reduced performance.
Usually you shouldn't care about gc at all because it does a really good job for you. There are usage scenarios where you want to get rid of a lot of objects at a certain point in time. Than it is feasible.
If you want to know garbage collection internals, you should read Hotspot Memory Management Whitepaper from Sun.
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).