Friday, 24 May 2013

Java Interview Questions - Garbage Collection


What is the purpose of garbage collection in Java, and when is it used?
Garbage collection is the automatic memory management used in Java to remove unused objects from memory. Objects allocated in heap, with the new operator cannot be deleted manually.

How is garbage collection implemented in your JVM of choice?

Does garbage collection guarantee that a program will not run out of memory?

How can you force garbage collection?
You can’t force GC but you can announce it: System.gc()

What comes to mind when you hear about a young generation in Java?
Garbage collection. Read more here

What comes to mind when someone mentions a shallow copy in Java?
Object cloning.

How can you minimize the need of garbage collection and make the memory use more effective?
Object pooling, mutable objects, weak references, avoid memory leaks caused by circular references.

Can an object’s finalize() method be invoked while it is reachable?
Yes, but not by the garbage collector.

Can an unreachable object become reachable again?
Yes, if the finalize method stores it’s reference in a reachable object.