StackTips

Frequently Asked Questions on Java Memory, JVM and Garbage Collection

sreepathy_ramanujam avtar
Ram   |   #Java   |   Sep 17, 2023, 
<img class="wp-image-10663 size-full" src="http://stacktips.com/wp-content/uploads/2016/06/jvm-heap.png" alt="jvm-heap" width="640" height="196" /> Java Heap sizes generated from http://gceasy.io.

What are different regions in JVM memory?

There are 5 regions.

  1. Eden
  2. Survivor 1
  3. Survivor 2
  4. Old (or Tenured) Generation
  5. Perm Generation (until Java 7). From Java 8 Perm Generation has been replaced with Metaspace.

Note: Eden, Survivor 1 and Survivor 2 are collectively called as Young Generation.

What is the purpose of each region in Java Memory?

  • Eden Generation: When an object is newly constructed it’s created in Young Generation. In most of the applicationsm most of the objects are short-lived objects. i.e. they will die soon. Thus they will get garbage collected within the Young Generation itself
  • Survivor: Objects that survive Minor GC are not directly promoted to the Old Generation. They are kept in the Survivor region for certain number of Minor GC collections. Only if they survive certain number of Minor GC collections then they are promoted to Old Generation.
  • Old (or Tenured) Generation: Certain objects tend to be long lived. Example: Application Context, HTTP Sessions, Caches, Connection Pools, …… Those long lived objects are promoted to old generation.
  • Perm Generation: This is the location where JVM objects such as Classes, Methods, String Interns…. are created.
  • Metaspace: Starting from Java 8 Perm generation has been replaced with Metaspace for performance reasons.

What are the different types of GCs?

There are 3 types of GCs:

  1. Minor GC: It’s also called as Scavenge GC. This is the GC which collects garbage from the Young Generation.
  2. Major GC: This GC collects garbage from the Old Generation
  3. Full GC: This GC collects garbage from all regions i.e. Young, Old, Perm, Metaspace.

When Major or Full GC run all application threads are paused. It’s called as stop-the-world events. In Minor GCs also stop-the-world event occurs but momentarily.

What are different types of GC algorithms?

Currently there are are 4 GC algorithms available in the Java Hotspot VM. They are The Serial GC, The Parallel GC, CMS and The G1.

  • Serial: The serial collector uses a single thread to perform all garbage collection work. It is best-suited to single processor machines, because it cannot take advantage of multiprocessor hardware. It’s enabled with the option -XX:+UseSerialGC.
  • Parallel: The parallel collector (also known as the throughput collector) performs minor collections in parallel, which can significantly reduce garbage collection overhead. It is intended for applications with medium-sized to large-sized data sets that are run on multiprocessor or multithreaded hardware. It’s enabled with the option -XX:+UseParallelGC.
  • CMS: The mostly concurrent collector performs most of its work concurrently (for example, while the application is still running) to keep garbage collection pauses short. It is designed for applications with medium-sized to large-sized data sets in which response time is more important than overall throughput because the techniques used to minimize pauses can reduce application performance. It’s enabled with the option -XX:+UseConcMarkSweepGC
  • G1: G1 is the latest garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with high probability, while achieving high throughput. Whole-heap operations, such as global marking, are performed concurrently with the application threads. It’s enabled with the option -XX:+UseG1GC

Another interesting article on Pros and Cons of each GC algorithm can be found here.

What tools are used for analyzing Garbage Collection logs?

  1. IBM Pattern Modeling and Analysis Tool for Java Garbage Collector
  2. GCEasy, an universal garbage collection log analyzer tool
  3. Oracle’s Visual GC, Java Mission Control
sreepathy_ramanujam avtar

Ram

Every single day millions & millions of people in North America - bank, travel and do commerce using the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on the scalability, availability, performance topics. Recently he has founded a startup, which specializes in troubleshooting performance problems.