Skip to main content

What is Java Garbage Collection? Summary of Mark and Sweep, Young, Old, Perm concepts

  • When doing Java programming, you often hear the term Garbage Collection. Although development is possible without this knowledge in the early stages of development, it is good to know to optimize performance or prevent memory leaks.
  • This is one of the topics frequently asked in interviews at companies that use Java.
  • In this document, we will summarize Java Garbage Collection so that you can easily understand it.
  • This is a brief summary to help you understand unknown concepts, so we recommend that you read other articles for detailed information.

What is Garbage Collection?

  • The name Garbage Collection means collection of garbage. Automatically collects unused memory
    • Unused Memory: Memory that is no longer used.
    • Memory Collection: Frees up unused memory so it can be used again.
  • Java memory management is done through JVM.
    • In existing languages, the programmer had to handle memory allocation and deallocation directly. If memory is not released properly, unused memory continues to accumulate and a memory leak occurs.
    • JVM is a virtual machine, processes memory allocation and deallocation automatically without the user having to worry
    • Specify the memory size to be used by the JVM by giving a JVM option when running the program.
    • Memory leak at the OS level is prevented by not directly accessing the OS level memory.
  • Garbage Collection secures memory by removing unreachable objects from the heap area
    • unreachable object: an object that is no longer referenced

What is Mark and Sweep?

  • characteristic
    • Garbage collection is done using the Mark and Sweep method
    • The memory state after garbage collection is a state in which memory space is secured.

Mark

  • Finds and marks a Reachable object. Garbage Collector scans all variables in the stack to find which object each references.
  • All threads are stopped for Mark work, this is called stop the world
  • For the above reasons, you can explicitly cause garbage collection by calling System.gc(), but this is not recommended.

Sweep

  • characteristic
    • Remove unmarked objects. Remove Unreachable Object

Heap area - Young (Eden, Survivor), Old, Perm

  • Heap area consists of Young Generation, Old Generation, and Perm areas
  • Consists of a total of 5 areas
    • Young Generation: Eden, Survivor 0, Survivor 1
    • Old Generation
    • Perm Region

image

Young Generation

  • Composed of Eden area, Survivor 0, and Survivor 1 area
  • Eden Realm
    • Where the newly created object is located
    • Minor GC occurs when the Eden area is full
  • Survivor Zone
    • Where surviving objects in the Eden realm are located
    • There are two areas, S0 and S1, one of which is empty and the other contains surviving objects.
    • Move surviving objects from Eden area and non-empty Survivor area to empty Survivor area.
    • The age value increases when moving from one survivor area to another survivor area.
    • Move as follows
      • There are surviving objects in S0, and S1 is empty -> Move the Eden area and the surviving objects in S0 to S1
      • There are surviving objects in S1, and S0 is empty -> Move the Eden area and the surviving objects in S1 to S0
    • When moving, the age value increases and surviving objects are moved to another survivor area.

Old Generation

  • characteristic
    • Objects that survive in the Young Generation are moved to the Old Generation when their age value exceeds a certain value.
    • The above step is called Promotion

Perm zone

  • characteristic
    • Where metadata, class information, etc. are stored
    • Changed to Metaspace starting from Java 8

Minor GC & Major GC & Full GC

  • GC is divided into Minor GC, Major GC, and Full GC depending on the heap area being organized.

Minor G.C.

  • characteristic
    • GC occurring in Young Generation
    • Move surviving objects from the Eden zone to the Survivor zone
    • When Survivor area is full, move surviving objects to Old Generation
  • order
    • Minor GC occurs when the Eden area is full
    • Move surviving objects from Eden area and Survivor area to empty Survivor area
      • Eden + S0 -> S1 or Eden + S1 -> S0
    • The age value increases when moving

Major G.C.

  • characteristic
    • GC occurring in Old Generation
    • Major GC occurs when the Old Generation area is full.
    • Removes unreachable objects from Old Generation area

Full GC

  • characteristic
    • Perform GC throughout Young Generation, Old Generation, and Perm areas
    • When Full GC occurs, all threads are stopped.
    • When Full GC occurs, Major GC, Minor GC, and Perm area GC all occur.

source