Garbage collection in Java is a process through which unnecessary objects are removed. Ah it seems easy but here comes the process:
Java does garbage collection based on JVM generations that is there are 3 layers young generation, old generation, perm generation (or meta space in Java8)
Young Generation is where most of the newly created objects are located and is a part of the heap memory. Young generation is divided into two parts:
Eden Space is the area where objects created using new keyword exist.
Survivor space is the area where objects who survived the garbage collection from Eden space exist.
Old Generation is where the objects that survived the garbage collection in the previous phase are kept. This is also part of heap memory.
Perm Generation (Meta Space) is where the classes and the objects (static variables) are stored. Perm Generation is a part of the non heap memory. The difference between Perm Generation (before Java8) and MetaSpace (Java8) is that Meta space resizes dynamically.
Tools for analysis:
In order to analyse the parts of the code which are occupying more memory, a tool provided by Oracle is called: JMC (Java mission control). This tool is available in the Java development kit.
In order to analyse the JVM heap memory, a tool provided by Oracle is called: Java Visual VM and is also a part of the JDK from version 6 and above.