Garbage collection
Java developers have long enjoyed the JVM’s ability to manage memory, including allocation and deallocation. The allocation component of memory management is straightforward and not wrought with inherent problems. The area that is most important is freeing previously allocated memory that is no longer needed by the application. This is referred to as deallocation or garbage collection. While not unique to Java programming language, its JVM does a wonderful job with garbage collection. This section takes a detailed look at JVM’s garbage collection.
The garbage collection process
The following example is the creation of two objects, making them reference each other, and then nullifying them both. Once they are nullified, they are no longer reachable, despite that they reference each other. This makes them eligible for garbage collection:
public class Main { public static void main(String[] args) { ...