DEVFYI - Developer Resource - FYI

Can we force Garbage collection?

Java Interview Questions and Answers (part 3)


(Continued from previous question...)

428. Can we force Garbage collection?

java follows a philosophy of automatic garbage collection, you can suggest or encourage the JVM to perform garbage collection but you can not force it. Once a variable is no longer referenced by anything it is available for garbage collection. You can suggest garbage collection with System.gc(), but this does not guarantee when it will happen. Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.

(Continued on next question...)

Other Interview Questions