Performance Analysis Java— Part 2 (Memory Analysis)

Rishabh Jain
4 min readOct 16, 2019

--

Hope you all like the first part of Java Performance Analysis. I am back with part 2 in which I will cover basics of memory analysis for Java applications (Java Memory Management, Garbage Collection, Garbage Collection Algorithms).

Java Memory Management

At high level JVM heap memory is divided into two parts — Young and Old Generation.

Young Generation: Store for newly created objects. Contain two parts:

Survivor Memory Spaces (Two blocks S0 and S1)

Eden Memory Spaces

When young generation is full, garbage collection is performed (i.e Minor GC)

Few points about Minor GC:

  1. Newly created objects are located in Eden space.
  2. When Eden is full, Minor GC is performed and all objects moved to one of survivor spaces. (At the same time Minor GC will check survivor objects and moved to other survivor space — At a time one of survivor space is always empty)
  3. Objects that survived after many Minor GC cycles moved to old generation. We can set a threshold age based on which we can allow to move survivor objects to old generation)

Old Generation : Objects that are long lived(or we can keep references for a longer time) and survived many cycles of Minor GC. Major GC is performed when old generation is full and usually takes long time. During Major GC , STOP THE WORLD EVENT occurs.

STOP THE WORLD EVENT ?

During any GC (Minor or Major), all applications threads are stopped until operation completes and during this time our application become unresponsive. This is called STW.

Minor GC is very fast and usually don’t affect the application in terms of unresponsiveness ( Still we should plan for correct Young Generation Memory)

Major GC is time consuming and will usually affect the application performance in worse way but making it unresponsive for large amount of time. During Major GC we mostly get timeout errors.

Reasons for frequent Major GC’s:

  1. Memory Leaks ( Object is referenced even its is not used and taking space in memory even after Minor/Major GC).
  2. Wrong Capacity Planning( Allocating less heap than required to keep application state healthy)

Now we have used Garbage Collection or GC many times but actually what it is?

Its is the process of removing Unused or De-referenced objects from memory during Minor/Major GC to make space for new objects. Garbage Collection is done by Garbage Collector (Background process that cleans Dereferenced objects)

Basic Steps involved in GC involves are:

  1. Mark — Marking unused/ dereferenced objects.
  2. Normal Deletion — Deleting dereferenced objects.
  3. Deletion with Compacting — After deleting unused, moving or clubbing all the survivor objects together to improve performance.

Time taken for Major GC is mainly dependent on Garbage Collection Alogorithms. Few are listed below:

  1. Serial GC (-XX:+UseSerialGC): Uses simple mark-sweep-compact ( marking — deleting-compacting) for Minor and Major. Suitable only for low memory applications — As compacting all the survivor to the start the memory takes time.
  2. Parallel GC (-XX:+UseParallelGC): Same as Serial GC , just uses multiple threads (Depends on Number of CPU Cores) for Minor GC and single thread for Major GC. Also know as throughput collection as it speeds up using multiple CPU.
  3. Parallel Old GC (-XX:+UseParallelOldGC) — Multiple threads for both Old and Young Generation
  4. Concurrent Mark Sweep (CMS) Collector (XX:+UseConcMarkSweepGC): — Low pause collector . Reduces GC time by doing most of GC work with application threads for Old Generation. Working same as Parallel collector for Young Generation.
  5. G1 Garbage Collector (-XX:+UseG1GC): This is totally different from all other GC Algorithms and available from Java 7. Here Heap is not diving into different parts like Eden, Survivor and Old. Instead it split the heaps into several hundred small regions. These regions are of fix size (2MB by default). G1 GC have two major advantages: a) Most of work can be done concurrently( No STW event) b) uses non contiguous space that help to deal with high heap. With Java 9 G1 GC is by default available.
Simple Heap Collector
With G1 GC — Each block of 2MB inspite of EDEN, SURVIVOR and OLD

Although I have planned to include GC Log Analysis and Heap Dump Analysis in this part but it will long then. I will cover all memory related analysis and recording in next part. In case of any query please connect with me https://www.linkedin.com/in/rishabh-jain-a5978583/

--

--

Rishabh Jain

Full Stack Developer — React, Node, Mongo DB, Postgres, RabbitMQ, AWS, Native Performance Engineering, Lambda, Javascript, Kubernetes, Docker.