Techdozo
  • Microservices
    • All
    • GraphQL
    • gRPC
    • Spring Boot
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    Distributed transaction in microservices using Saga

    Distributed Transactions in Microservices: implementing Saga with Temporal

  • Spring Boot
    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful API Gateway with gRPC

    RESTful API Gateway with gRPC

  • gRPC
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    gRPC Interceptor: unary interceptor with code example

    gRPC Interceptor: unary interceptor with code example

    gRPC: synchronous and asynchronous Server streaming RPC

    gRPC: synchronous and asynchronous Server streaming RPC

    Photo by Ramón Salinero on Unsplash

    gRPC: synchronous and asynchronous unary RPC in Java

    Microservices inter-process communication using gRPC

    gRPC for microservices communication

  • GraphQL
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping

    Spring for GraphQL : @SchemaMapping and @QueryMapping

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

  • Kubernetes
    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Components of Kubernetes Architecture

    Components of Kubernetes Architecture

    Helm Chart: quick start your app deployment on Kubernetes

    Helm Chart: quick start your app deployment on Kubernetes

    gRPC load balancing on Kubernetes (using Headless Service)

    gRPC load balancing on Kubernetes (using Headless Service)

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

  • Java
    Java Streams: Stream Operation with Examples

    Java Streams: Stream Operation with Examples

    Java Streams: stream creation with examples

    Java Streams: stream creation with examples

    Garbage Collection

    Super Fast Garbage Collectors in Java

    Calculus

    Functional Programming in Java

No Result
View All Result
  • Login
  • Microservices
    • All
    • GraphQL
    • gRPC
    • Spring Boot
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    Distributed transaction in microservices using Saga

    Distributed Transactions in Microservices: implementing Saga with Temporal

  • Spring Boot
    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful API Gateway with gRPC

    RESTful API Gateway with gRPC

  • gRPC
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    gRPC Interceptor: unary interceptor with code example

    gRPC Interceptor: unary interceptor with code example

    gRPC: synchronous and asynchronous Server streaming RPC

    gRPC: synchronous and asynchronous Server streaming RPC

    Photo by Ramón Salinero on Unsplash

    gRPC: synchronous and asynchronous unary RPC in Java

    Microservices inter-process communication using gRPC

    gRPC for microservices communication

  • GraphQL
    GraphQL Error Handling

    GraphQL Error Handling

    Pages

    Spring for GraphQL: Pagination with Code Example

    Spring for GraphQL: Interfaces and Unions

    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping

    Spring for GraphQL : @SchemaMapping and @QueryMapping

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

  • Kubernetes
    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Components of Kubernetes Architecture

    Components of Kubernetes Architecture

    Helm Chart: quick start your app deployment on Kubernetes

    Helm Chart: quick start your app deployment on Kubernetes

    gRPC load balancing on Kubernetes (using Headless Service)

    gRPC load balancing on Kubernetes (using Headless Service)

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

  • Java
    Java Streams: Stream Operation with Examples

    Java Streams: Stream Operation with Examples

    Java Streams: stream creation with examples

    Java Streams: stream creation with examples

    Garbage Collection

    Super Fast Garbage Collectors in Java

    Calculus

    Functional Programming in Java

No Result
View All Result
Techdozo
No Result
View All Result
Home Java

Super Fast Garbage Collectors in Java

Bhavik Banchpalliwar by Bhavik Banchpalliwar
September 14, 2020
in Java
Reading Time: 9 mins read
0
A A
0
Garbage Collection

Garbage Collection

ADVERTISEMENT

Introduction

GarbageCollection: There are many technical definitions but in layman terms, it’s just an automated way to collect un-referenced or un-used (garbage) objects from memory and efficiently utilize the memory available for the application.

Urghh!! Yet another technical definition..

From here on let’s save some space and use “GC” as an abbreviation for Garbage Collection.

ADVERTISEMENT

Evolution of GC

Before Java, with c or c++ we had to explicitly allocate or de-allocate the memory using malloc()/realloc()/calloc()/free()/destructors etc. Since we had to manage the memory deallocation explicitly, any mistake would make the applications prone to memory leaks. With the emergence of Java, the concept of automatic GC was introduced.

Glossary

Throughput: Average amount of time spent while running the application vs average time spent in GC

Latency: Amount of time code pauses during GC

Heap: Region in memory where java objects are stored.

Young generation: Region in heap where newly created objects are stored.

Old generation (tenured): Region in heap where long lived objects are stored.

Minor GC: Most of the objects in java are short lived. It means they are created and they die early. So most of the objects in young generation are collected with Minor GC. It’s called Minor because, it’s less expensive on the application and doesn’t add extra pauses or latencies.

ADVERTISEMENT

Major GC: The objects which survive after minor GC are moved from young generation to old generation space and once the old generation space reaches its threshold Major GC is triggered, which in most of the cases is stop the world (STW) operation and adds overhead on the application by pausing all the threads and contributes to latency. Hence understanding the different types of GC and choosing the correct one for our application is very important.

So, enough done with these basic terms and definitions. We are here to understand SuperFast garbage collector, isn’t it ?? But wait !!! How do we quantify SuperFast if we don’t know how fast existing GC mechanisms are. Let’s spend a couple more minutes discussing the existing GC mechanisms.

Existing GC

GC works in 2 simple steps, Mark and Sweep. Mark marks the objects eligible for garbage collection. Sweep removes the objects marked by the “Mark” step.

Serial GC

Uses a single application thread to perform GC and freezes all application threads (STW), which makes it not so suitable for low latency applications and multi-threaded applications.

java -XX:+UseSerialGC -jar Application.java

Application which can afford some application pause time can enable this GC using the following argument. Mostly to be used for single threaded applications.

Parallel GC

Well this is the default GC of the JVM and unlike Serial GC, uses multiple threads to perform GC hence it’s faster than Serial GC but again freezes other application threads while running (STW). This can be enabled as:

java -XX:+UseParallelGC -jar Application.java


Suitable for Multi threaded applications.

However, if we want to use this GC we can use different options to minimize the impact of STW.

-XX:ParallelGCThreads : Parallel threads to be used for GC

-XX:MaxGCPauseMillis: Max pause time during GC. In order, to meet this SLA, GC can make some adjustments to other parameters.

-XX:GCTimeRatio: Maximum Throughput required.

CMS (Concurrent Mark & Sweep)

This uses multiple garbage collector threads for garbage collection. It’s designed for applications that prefer shorter garbage collection pauses. If more than 98% of the total time is spent in CMS garbage collection and only less than 2% of the heap is recovered, then an OutOfMemoryError is thrown by the CMS collector. If necessary, this feature can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line. CMS can be enabled using

java -XX:+UseConcMarkSweepGC -jar Application.java


Less pause time as compared to Serial and parallel GC

CMS is deprecated post Java 9 and throws a warning if we use it. In Java 14, it’s completely dropped.

G1 GC

G1 stands for Garbage first. Available since JDK7, unlike other collectors, G1 collector partitions the heap into a set of equal-sized heap regions, each a contiguous range of virtual memory. During Marking phase, G1 shows a concurrent global marking phase to determine the liveness of objects throughout the heap. During sweeping, G1 knows which regions are mostly empty. It collects in these areas first, which usually yields a significant amount of free space. It is why this method of garbage collection is called Garbage-First.

G1 is designed for applications running on multi-processor machines with large memory space. It’s more performance efficient. It can be enabled as :

java -XX:+UseG1GC -jar Application.java


Low GC pause time for high heap applications

Phew !! Finally, it’s time to read about Super fast garbage collectors !!!

Super fast GC

Until now all the JDK GC’s are stop the world operations having issues with application latencies. With the latest versions of Java ( > JDK11), two new GC have been introduced, Shenandoah and ZGC. These GC’s allows java applications to run while it collects the garbage concurrently. Let’s have a look.

Shenandoah

Introduced in JDK12, Shenandoah’s key advance over G1 is to do more of its garbage collection cycle work concurrently with the application threads. G1 can evacuate its heap regions, that is, move objects, only when the application is paused, while Shenandoah can relocate objects concurrently with the application. To achieve the concurrent relocation, it uses what’s known as a Brooks pointer. This pointer is an additional field that each object in the Shenandoah heap has and which points back to the object itself.

Shenandoah does this because when it moves an object, it also needs to fix up all the objects in the heap that have references to that object. When Shenandoah moves an object to a new location, it leaves the old Brooks pointer in place, forwarding references to the new location of the object. When an object is referenced, the application follows the forwarding pointer to the new location. Eventually the old object with the forwarding pointer needs to be cleaned up, but by decoupling the cleanup operation from the step of moving the object itself, Shenandoah can more easily accomplish the concurrent relocation of objects.

The GC pause times are consistent with varying heap sizes i.e. A pause time with 200 MB heap would be same as 200GB heap. Yes you read it right !!

To use Shenandoah in your application from Java 12 onwards, enable it with the following options:

java -jar -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC Application.java

If you haven’t yet migrated to JDK 12, Shenandoah supports back ports to JDK 8 and JDK 12. Also, Shenandoah isn’t enabled in the JDK builds that Oracle ships, but other OpenJDK distributors enable Shenandoah by default

Use Shenandoah when low latencies are desired and application migrated to OpenJDK

ZGC

Introduced as an experimental feature in Java 11, ZGC is low latency and scalable garbage collector. ZGC has a consistent GC pause time of 2ms and a guaranteed max GC pause time of 10 ms, which makes it Super Fast and works with a heap in sizes of TB’s. Yes, in TB’s !!

ZGC pause time does not increase with an increase in heap size. ZGC allows a Java application to continue running while it performs all garbage collection operations except thread stack scanning. It also supports concurrent class unloading. Performing concurrent class unloading is complicated and, therefore, class unloading has traditionally been done in a stop-the-world pause. Determining the set of classes that are no longer used requires performing reference processing first. Reference processing is expensive and in the worst case would require the whole heap to be scanned. Well, ZGC does all this concurrently, hence there is no penalty on latencies during class unloading. ZGC can be enabled in the below manner:

java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -jar Application.java

What is “Z” in ZGC ? Nothing it’s just a “Z” !!

Below are some comparative studies published by SPECJbb

ZGC is a good fit for applications that require large amounts of memory, such as with big data. However, ZGC is also a good candidate for smaller heaps that require predictable and extremely low pause times.

Summary

Thats all about the new super fast garbage collectors. They are super fast because they perform all the mark and sweep concurrently without stalling the application threads. Choose the garbage collector depending on your application needs. Don’t forget to benchmark your application’s performance with different GC mechanisms, you will be blown away to see the impact one JVM switch can have.

Hope these 2 cents on GC were helpful !!

Tags: garbage collectiongcjava
Previous Post

Functional Programming in Java

Next Post

Deploying Microservices Locally with Docker and Kubernetes

Bhavik Banchpalliwar

Bhavik Banchpalliwar

In Main thread, Software Engineer @Amazon and in parallel thread, pursuing MTech from BITS Pilani

Related Posts

Java Streams: Stream Operation with Examples
Java

Java Streams: Stream Operation with Examples

May 8, 2021
Java Streams: stream creation with examples
Java

Java Streams: stream creation with examples

May 1, 2021
Calculus
Java

Functional Programming in Java

September 7, 2020

Discussion about this post

Recent Articles

GraphQL Error Handling

GraphQL Error Handling

August 9, 2023
Pages

Spring for GraphQL: Pagination with Code Example

July 26, 2023

Spring for GraphQL: Interfaces and Unions

May 17, 2023
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
  • Trending
  • Comments
  • Latest
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
Deploying a RESTful Spring Boot Microservice on Kubernetes

Deploying a RESTful Spring Boot Microservice on Kubernetes

August 18, 2021
gRPC Interceptor: unary interceptor with code example

gRPC Interceptor: unary interceptor with code example

April 30, 2022
Temporal Workflow Orchestration

Workflow Orchestration with Temporal and Spring Boot

October 29, 2022
Calculus

Functional Programming in Java

0
Java Streams: stream creation with examples

Java Streams: stream creation with examples

0
Garbage Collection

Super Fast Garbage Collectors in Java

0
Java Streams: Stream Operation with Examples

Java Streams: Stream Operation with Examples

0
GraphQL Error Handling

GraphQL Error Handling

August 9, 2023
Pages

Spring for GraphQL: Pagination with Code Example

July 26, 2023

Spring for GraphQL: Interfaces and Unions

May 17, 2023
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
Facebook Twitter Pinterest

TECHDOZO

Simplifying modern tech stack!

Browse by Category

  • Bitesize
  • GraphQL
  • gRPC
  • Java
  • Kubernetes
  • Microservices
  • Spring Boot

Recent Articles

GraphQL Error Handling

GraphQL Error Handling

August 9, 2023
Pages

Spring for GraphQL: Pagination with Code Example

July 26, 2023

© 2023 Techdozo.

Welcome Back!

Sign In with Google
OR

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • gRPC
  • Kubernetes
  • Microservices
  • GraphQL

© 2023 Techdozo.