Source: Canva Team

Garbage, Garbage Go Away

What, Why & How of Java’s Garbage Collection

Any application needs memory to run. However, computer memory has limited space. So, in order to run applications, it is important that the memory is cleaned once in a while to make space for new data and remove the old unused data.

Java Memory Model
Java Memory Model
Garbage
Source: Oracle.com
  1. Sweeping: In this step, the memory allocated by Objects which are not marked in the previous step is freed up.
  2. Compaction: The objects which survive sweeping are moved into a single contiguous block of memory. This prevents the heap from becoming fragmented over time and allows easier & faster allocation of new objects.
Mark & Sweep Garbage Collection
  1. Old Generation: The older objects (which have survived in the application for a long) are stored in the old generation.
Generations of Heap
  1. If an object survives, then it is likely to live forever.
  2. Mark & sweep process takes less time to run when there is a lot of garbage i.e. the marking will be quicker if the area to be analyzed is small and it comprises mostly dead objects.
Generational Garbage Collection
  1. When Eden space fills up, Minor Gargabe Collection takes place. Minor Garbage Collection is the process in which the mark and sweep process runs in the young generation.
  2. As a result of Minor GC, the live objects are moved to one of the survivor spaces (let’s say S0). Dead objects are permanently removed from young generation.
  3. Once again, as the application runs, the Eden space starts filling up with new objects. In the next Minor Garbage collection cycle, the young generation and S0 are cleared out of dead objects. The surviving objects are moved to S1 this time and their age is incremented (to mark that they have survived one garbage collection).
  4. At the next minor GC, the same process repeats. However this time the survivor spaces switch. Alive objects are moved to S0 and they are aged. Eden and S1 are cleared.
  5. Objects are copied between survivor spaces in this way until they’ve been copied a certain number of times (i.e. they have survived a certain number of Minor GC cycles) or there isn’t enough space left there. These objects are then copied into the Old region. This process is called Aging.
  6. Major Garbage Collection: In this step, the mark and sweep process is executed in the old generation to clear out dead objects from there. Major GC is slower compared to Minor GC as the old generation mostly comprises live objects (Hence marking takes time).
Generational Garbage Collection in Action
JVM Flags
Java Garbage Collectors
  1. How much time it takes for one garbage collection cycle to run
  1. To Enable GC logging in an application, add below JVM parameter
-XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -Xloggc:/tmp/[Application-Name]-[Application-port]-%t-gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=20 -XX:GCLogFileSize=100M
[15,651s][info ][gc] GC(36) Pause Young (G1 Evacuation Pause) 239M->57M(307M) (15,646s, 15,651s) 5,048msExplanation:[Time from when the application was started = 15,651s][Log level=info][Tag=gc][GC identification number = 36] [Type of GC= Pause Young] [Cause for stating the GC= G1 Evacuation Pause] [Memory Consumption Information : "used before GC" = 239M -> "used after GC" = 57M (Heap Size=307M)] [Start and End times for the GC = 15,646s, 15,651s] [Total GC Duration= 5,048ms]

Software Developer | Technical Writer | Technology Enthusiast