- Garbage Collection Is The Process Of Looking At Heap Memory, Identifying Which Objects Are In Use And Which Are Not, And Deleting The Unused Objects.
- An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
- In java the process of garbage collection is automated.
STEPS OF GARBAGE COLLECTION:
STEP-1 (MARKING)
The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.

STEP 2:( NORMAL DELETION)
Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.

STEP-3: (COMPACTING)
To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster.

GENERATIONAL GARBAGE COLLECTION:
The partition of objects into different generations (time intervals) based on time of allocation, and giving them different GC policies depending on age. Based on the heuristic that most objects are discarded shortly after being used–hence the GC is tuned to get rid of those first.

Here is an example of such data. The Y axis shows the number of bytes allocated and the X access shows the number of bytes allocated over time.
As you can see, fewer and fewer objects remain allocated over time. In fact most objects have a very short life as shown by the higher values on the left side of the graph.

