Java Garbage Collection
TLDR: Java Garbage Collection is an automated memory management feature introduced with the Java Programming Language in 1995 by Sun Microsystems. It automatically reclaims unused memory, preventing memory leaks and reducing developer effort in manual memory management. By identifying and removing Java Objects no longer in use, the Garbage Collector ensures efficient utilization of the heap memory within the Java Virtual Machine (JVM). This process operates in the background and supports Java's principle of reliability by maintaining program stability even under dynamic memory conditions.
https://docs.oracle.com/javase/specs/jvms/se20/html/jvms-2.html
Java Garbage Collection employs different algorithms, such as Mark-and-Sweep, Generational Garbage Collection, and G1 Garbage Collector, to manage memory efficiently. Generational Garbage Collection divides memory into young, old, and permanent generations to optimize the collection process. For instance, short-lived objects are quickly discarded in the young generation, while long-lived objects are stored in the old generation. Modern Garbage Collectors like ZGC (introduced in 2018) and Shenandoah (introduced in 2019) focus on low-latency operations, making them suitable for large-scale, high-performance applications.
https://docs.oracle.com/en/java/javase/20/gctuning/introduction.html
One of the unique features of Java Garbage Collection is its ability to integrate with tools like the Java Flight Recorder and Java Mission Control for monitoring and tuning garbage collection processes. These tools provide insights into memory allocation patterns and help optimize the application's performance. However, while Garbage Collection simplifies memory management, it may introduce occasional performance hiccups, known as Stop-the-World Pauses, where the JVM temporarily halts application threads to perform garbage collection. Developers can mitigate this by fine-tuning Garbage Collector settings or choosing an appropriate collector for their application's workload.
https://docs.oracle.com/en/java/javase/20/troubleshooting/performance-gc.html