"System.gc()" doesn't really do anything. it SUGGEST to the JVM that now might be a good time to do GC. However, the JVM is free to ignore your suggestion. The JVM should know about how much memory it has free. If it still has 90% available, and you say "DO A GC NOW!!!", the smart money would be on it saying "Meh - why waste my time?".
So, in this program, you have no guarantee that a GC ever happens.
In your first example, when you set gc = null, that object is ELIGIBLE for collection, but if the garbage collection never happens, it won't be.
When the main method ends, the JVM shuts down, and ALL that memory is returned to the OS. There may be some strange cases where a thread can continue to run after main() ends, but that is not the case here.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
"Memory allocated for the object 'gc' on heap reclaimed ?"
If "finalize" got executed then you can be sure that the memory is deallocated.
in the other case, if "gc" not made null the finalize is not executed. "May i assume it as 'The memory allocated to gc not reclaimed even after the completion of the
main method' ".
You HAVE called System.gc() IN the main method itself so you assumption of "Completion" of main method is wrong. When the System.gc() was called the main method has not completed. And because you haven't made the reference as null, which results in an active reference to the object and hence it is ineligible for garbage collection.
"Will the finalize() surely executed before the object's deallocation ?"
YES, the finalize() will surely be called before an object is deallocated but there is NO SURETY that an object will be deallocated on calling of System.gc() as stated by Fred.