What is Garbage Collection

In Java you create objects by using new operator, these objects occupy heap memory. Java don't give any way for a programmer to explicitly delete the object or to reclaim their memory. The process of reclaiming memory from unused objects is handled by JVM itself. And this process where JVM identifies unused objects and delete them to free up memory is called Garbage Collection. This is automatic process. The part of JVM that handles Garbage collection is called Garbage Collector (GC). GC runs as a separate thread in JVM, a demon thread.
Thus garbage collector provides an automatic solution to memory management in Java and as a programmer you, in most cases, don't have to write code/logic for that. It is also important to mention that GC runs only on heap memory.

When an object becomes eligible for Garbage Collection?

An object becomes eligible for garbage collection when it is no longer referenced by any part of your program. Or in other words we can say an object becomes eligible if there is no reference variable that refers to the object. This kind of object is also called unreferenced or unreachable or unused object.
Also, if some object is reference by a reference variable and the reference variable itself is not reachable from any live thread that also makes the object eligible. This kind of scenario happens in 'island of isolation'. Ex: say there are 3 reference variables ref1, ref2, ref3 and 3 objects obj1, obj2, obj3. If ref1 points to obj1 which has an instance variable ref2 that points to object 2 which further has instance variable ref3 that points to obj3. But there is no way ref1 can be reached by a live thread. In such case all the 3 objects become eligible for GC.

Make an object eligible for Garbage Collection explicitly

There are 3 ways of doing that:
  • Nulling a reference
  • Reassigning a reference variable
  • Isolating a reference
  • Objects created inside a method, becomes eligible when method returns, unless the object is returned from method.

What about Garbage collection of Strings

The Strings that are created using new and go in heap are treated as normal objects. However, garbage collection of the String literals which are part of String pool is pretty tricky and may differ across various JVMs. Normally the String constant pool is part of 'permGen' which is garbage collected but very rarely.

When does Garbage Collector run?

JVM run garbage collector when it feels there is a lack of heap memory. It is advised not to rely on garbage collector as to when it will run and which objects it will delete (even if an object is eligible for GC it might not be deleted when GC run).
The only thing that is guaranteed about Garbage Collector is when JVM is running very low on heap memory garbage collector will run and it always run before it throws OutOfMemoryException.

Can we force Garbage Collector to run?

No. Garbage Collection can't be forced.
Though you can request JVM to run garbage collector using below methods:
System.gc(), Runtime.getRuntime.gc()
Note: With Java 6 onwards, garbage collection has evolved so much that it recommended not to invoke above methods, instead leave it to JVM.

What is finalize() method?

Before an object is cleaned by Garbage Collector, finalize() method of that object is invoked. This might seem useful to write some important code (ex: to free-up resources that are held by your object) in this method that can be executed before the object is deleted. But, since you can’t rely on garbage collector to delete the object, it's not advised to override this method.