Wednesday, June 18, 2008

Java Garbage Collection

What do you know about the garbage collector ?
The JVM's heap stores all objects created by a Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. The garbage collector runs finalizers on objects, since it is not generally possible to predict exactly when unreferenced objects will be garbage collected, it is not possible to predict when object finalizers will be run. Programmers should avoid writing code for which program correctness depends upon the timely finalization of objects.


How can you force garbage collection?
You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

No comments: