In languages like C/C++, it is the duty of the programmer to create and destroy objects. But if the programmer does not perform his duty, sufficient memory may not be available for the creation of a new object and the program may terminate causing **OutOfMemoryErrors**.
Java relieves the programmer from memory management task and itself reclaims the memory occupied by the objects which are no longer in use. Garbage Collection in Java is carried out by a daemon thread called **Garbage Collector**. **JVM(Java Virtual Machine)** invokes it when there is lack of memory(heap) for new objects.
* Cyclic dependencies are not counted as a reference so if Object X has a reference of Object Y and Object Y has a reference of Object X and they don’t have any other live reference then both Objects X and Y will be eligible for Garbage Collection.
* An object is also eligible for garbage collection when its reference variable is re-assigned and that object no longer has any other reference to it. Look at the code snippet below:
```java
Integer i1 = new Integer(10); //new Integer object created
Integer i2 = new Integer(20); //Another new Integer object created
i1 = i2;//i1 now references to i2 (re-assigned), therefore 10 (object in the heap) becomes eligible for garbage collection since it no longer has any other reference to it
## How to manually make an object eligible for Garbage Collection?
* Even though it is not the task of the programmer to destroy the objects, it is a good programming practice to make an object unreachable(thus eligible for GC) after it is used.
* There are generally four different ways to make an object eligible for garbage collection.
4. [Island of Isolation](http://www.geeksforgeeks.org/island-of-isolation-in-java/)
## Ways of requesting JVM to run Garbage Collector<sup>1</sup>
* Though making an object eligible for Garbage Collection, it depends on sole discretion of JVM to run the Garbage Collector to destroy it.
* We can also request JVM to run Garbage Collector. There are two ways to do it :
1. Using _**System.gc()**_ method : System class contain static method gc() for requesting JVM to run Garbage Collector.
2. Using _**Runtime.getRuntime().gc()**_ method : Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run Garbage Collector.
```java
// Java program to request
// JVM to run Garbage Collector
public class Test
{
public static void main(String[] args) throws InterruptedException
* Objects have resources associated with them. It is their responsibility to free the resources.
* The finalize(), is declared in Object class and is called by garbage collector once, just before destroying the object. An object can take any last action using this method just before its area is reclaimed by the garbage collector.
* finalize() method is present in Object class with the following prototype.
1. The finalize() method called by Garbage Collector, not JVM. Although Garbage Collector is one of the modules of JVM.
2. Object class finalize() method has an empty implementation, thus it is recommended to override finalize() method to dispose of system resources or to perform other cleanups.