Tuesday, December 4, 2018

Java Crash Course : Garbage Collection

In Languages like C, C++ it is the responsibility of the developer to allocate/deallocate the memory needed for a program using malloc and calloc commands.

In Java, it is done by Java virtual machine automatically using the Garbage collector. In general, any object on the heap which cannot be referenced from the stack is eligible for garbage collection.

Garbage Collection uses two strategies to clear the heap.

  1. Match and sweep - Instead of identifying the objects with no references, GC maps all the objects for which there is a reference from the stack and sweeps others.
  2. Generation Collection - It was discussed as part of Java Memory management crash course where the heap is divided into four sections. Objects from the young generation are swept more than the objects in the old generation.
Java provides two methods for interacting with Garbage collection.A developer has a minimum to no control on managing how and when to run garbage collection.

  1. System.gc() - Using which we can explicitly call garbage collection but there is no guarantee that it would execute. Further, while the garbage collector is running, all the threads are halted. i.e., the entire application is suspended.
  2. Finalize() - Object class has finalize method which can be overridden on your class as well to do some activity. Again, as in System.gc() there is no guarantee that this method is called when an object is garbage collected. The only valid use case is to see if the resources used are closed or not.

public void finalize()
log "object is discarded without releasing its resource"

If the maximum heap size allocated to the program is reached, the program will be terminated and java.lang.OutOfMemory error is thrown.

Java Crash Course : Memory Management in JVM

This document holds the summary of memory management concepts in JVM (Java Virtual Machine) that every software engineer should be aware of while designing/coding in Java.

Memory is divided into 2 parts
  1. Stack 
  2. Heap

Stack : Small when compared to heap memory and stores local variables and references to the objects in Heap.

  1. Each thread has its own Stack.
  2. Shorter life span
  3. Data from Stack is removed as soon as close braces ( } ) is encountered.
  4. Primitive types are saved in Stack.
  5. If a method pushed 10 elements to the Stack, all the 10 will be removed upon encountering closing braces.

Heap : This should be your main focus while designing/coding/debugging/testing the application.

  1. Long term memory.
  2. All objects are stored in the heap and references of those are saved on Stack.
  3. JDK provides jvisualvm application to analyse heap memory.
  4. In modern JVM heap is split into 2 types.
    1. Old Generation
    2. Young Generation - which in turn split into 3 types.
      • Eden
      • Survivor 0
      • Survivor 1
In older java versions 6 & 7, PermGen is also part of Heap memory which stores classes metadata and Strings which can never be garbage collected. From Java 8 PermGen/Metaspace is moved out of heap memory, and Strings are moved to Old Generation where they can be garbage collected.

Heap Memory to the Naked Eye

  • Above image shows the heap memory of a spring boot application where the maximum heap size set to 50mb.
  • For maven spring boot application.
    • set MAVEN_OPTS="-Xmx50m"
    • mvn spring-boot:run
  • Look how 50Mb is split among old, Eden, Survivor 1 and 2 memory.
    • Old Generation – 33.5 MB
    • Survivor 1 & 2 – 2.5 MB each
    • Eden – 15 MB
  • Objects are first allocated in Eden memory location if they survive the garbage collection, they are moved to survivor memory, then to Old generation. An object, in general, is moved to Old generation if it survives garbage collection more than eight times.
  • Garbage collector decides which memory should be cleared, i.e. Eden memory/ Old Gen memory based on the amount of memory it needs. In general, GC mostly clears the young memory and sometimes rarely old Gen.

Explore jvisualvm tool of JDK to monitor CPU, memory, threads, instances and get the heap dump.  Use eclipse memory analyser to see which class instances take up huge memory and which member variable is responsible for the memory leak.