I was reading about Garbage Collection and finalize() method of Java and there are some of the doubts that caught my mind. Sorry if you think that these doubts are really silly.
I was reading the article http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html. In this, point 5 says: 'Before removing an object from memory Garbage collection thread invokes finalize() method of that object and gives an opportunity to perform any sort of cleanup required'. So does this thing happens for sure? I mean will the finalize() method be always called before execution of Garbage Collector method?
How does the Garbage Collector knows that it needs to execute? For example, I have an application deployed on server, so when does the GC executes? Does it executes periodically, or when some (say 1MB) amount of garbage is collected and a trigger is executed or something or it is just random and there's no way to determine when will it execute?
How does it degrade the performance of my application since the garbage collection isn't happening?
Suppose I have a lots of garbage in my heap but the garbage collector isn't executed. If this happens, so isn't it a bad behavior or a flaw of JVM?
Can we say that garbage collection which is done manually in C/C++ better than in Java taking the consideration that we as a programmer are smart enough and know when we need to dispose off the 'not-so-referenced' pointers?
finalize
does not happen "for sure". There are several bugs in the way it was originally designed, making it fairly unreliable. – Hot Licks