0%

Java Garbage Collection Notes

Objective

Describe what Java garbage collaction is.

Requirement & Prerequisites

Machine

  • None

Human

  • Know what JVM, JRE, JDK are
  • Know stack and heap memory in Java

What is Garbage Collection (GC)?

  • Garbage collection is a mechanism in Java technology that automatically allocates and deallocates heap memory.
  • Garbage collection mechanism identify which objects are in use or not, and deleting the unused objects for the perfoemnce purpose

Why Garbage Collection (GC) is important?

Becuase gc can optimize the use of memory.

Without heavily using too much memory, our applcaitions/programs can run efficiently, which brings lot of benifits, like better user experience.

Heap memory structure


since GC it’s all about memory, let’s see what it looks like.

  • Young Generation
    • eden: any new objects are allocated to the eden space.
    • Suvivor space: a space thatm in use or referenced objects are moved to, when eden space is full —> minor gc occured
      • S0
      • S1
  • Old Generation: a space that in use or referenced objects are moved when one of the survivor space is full —> minor gc occured
  • Permanent Generation: contains metadata required by the JVM, Java SE library classes and methods may be stored here

How Garbage Collection (GC) Works?

Before taking about how garbage collection works. I want to explain what stop the world event is.

Stop the World Event

Stop the world event is an event that all application threads are stopped until the GC operation complete. So most of the GC events are stop the world events.

Types of Garbage Collection (GC)

Minor garbage collection (Minor GC)

An event when the young generation memory space is full, and JVM removes the unused objects and move the aged objects to old generation memory space.

Major garbage collection (Major GC)

An event when the old generation memory space is collected.
While minor GC continuing, the old generation memory space will also be full, so the collection is needed.

Full garbage collection (Full GC)

An event when the permanent generation memory space be collected

I will keep write more topic on GC in the future, to be continued… :)

Reference