In one of my interview, I had faced with below two questions. Though I had explained it, I think interviewer is not satisfied with it. For these two questions what are the important points to be conveyed?
Explain working of Java Virtual Machine (JVM) Garbage collection mechanism in Java [ September 15, 2005: Message edited by: Sachin Ramesh Vir ]
Executes byte code that is compiled with Javac. Bytecode can be reversed back JVM is implemented for all major systems, that's why Java is cross-platform - byte code is OS independant.
Garbage collection mechanism in Java
When there is no reference pointing to object, the object can be removed by GC. Though you can't tell the precise time when it be removed. GC usually runs in it's own thread and cleans memory periodically or when system is low on physical memory. You can ask GC to do system clean up right now - System.gc(). There is finalize method in every object. You can override it so it will get control when GC will determine the object will be deleted and before it is actually deleted.
1) Yes, Java programs can leak memory. That happens when program stores references it does no longer need. That might happen with event listeners and observer (etc). If you register your object as listener but then don't use it anymore it will not be deleted as event source will still try to notify that object and keep reference. Unless event source is also de-referenced. Or the object is removed from listeners list.
2) Circular references are also detected and deleted by GC. If your A references B, but nobody else references neither A nor B then both will get deleted.