A Definition of Java Garbage Collection
A Definition of Java Garbage Collection
Java garbage collection is the process by which Java programs perform automatic memory
management. Java programs compile to bytecode that can be run on a Java Virtual Machine,
or JVM for short. When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program. Eventually, some objects will no
longer be needed. The garbage collector finds these unused objects and deletes them to
free up memory.
While HotSpot has multiple garbage collectors that are optimized for various use cases, all
its garbage collectors follow the same basic process. In the first step, unreferenced
objects are identified and marked as ready for garbage collection. In the second step,
marked objects are deleted. Optionally, memory can be compacted after the garbage
collector deletes objects, so remaining objects are in a contiguous block at the start of the
heap. The compaction process makes it easier to allocate memory to new objects
sequentially after the block of memory allocated to existing objects.
Old Generation: Objects that are long-lived are eventually moved from the Young
Generation to the Old Generation. When objects are garbage collected from the Old
Generation, it is a major garbage collection event.
Permanent Generation: Metadata such as classes and methods are stored in the
Permanent Generation. Classes that are no longer in use may be garbage collected from
the Permanent Generation.