DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Ways To Reduce JVM Docker Image Size
  • Best Practices: Java Memory Arguments for Containers
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Monolith: The Good, The Bad and The Ugly
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • The Future of Java and AI: Coding in 2025
  1. DZone
  2. Coding
  3. Java
  4. Pitfalls in JVM and Docker Defaults

Pitfalls in JVM and Docker Defaults

Here are some of the common pitfalls among Docker defaults in the JVM.

By 
Petr Bouda user avatar
Petr Bouda
DZone Core CORE ·
May. 27, 19 · Presentation
Likes (11)
Comment
Save
Tweet
Share
25.6K Views

Join the DZone community and get the full member experience.

Join For Free

First, there are a lot of articles about JVM and container-awareness:

  • https://medium.com/@jnsrikanth/docker-support-in-java-10-fbff28a31827
  • https://blogs.oracle.com/java-platform-group/java-se-support-for-docker-cpu-and-memory-limits

In this post, I use Java 11, which means the default for garbage collector is supposed to be G1GC! Let's look at the defaults, which are automatically chosen by the JVM depending on memory size and provided CPUs.

Default GC Based on Docker CPU

$ docker run --cpus="2" openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize   = 5200936960    {product} {ergonomic}
     bool UseG1GC       = true          {product} {ergonomic}
     bool UseSerialGC   = false         {product} {default}
$ docker run --cpus="1" openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize    = 5200936960     {product} {ergonomic}
     bool UseG1GC        = false          {product} {default}
     bool UseSerialGC    = true           {product} {ergonomic}


Be aware of the number of CPUs in your Docker container; the type of garbage collector can be absolutely different.

Default GC Based on Docker Memory

$ docker run -m 2g openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize     = 536870912      {product} {ergonomic}
     bool UseG1GC         = true           {product} {ergonomic}
     bool UseSerialGC     = false          {product} {default}
$ docker run -m 1g openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize    = 268435456     {product} {ergonomic}
     bool UseG1GC        = false         {product} {default}
     bool UseSerialGC    = true          {product} {ergonomic}


In the snippets above, the JVM automatically changed the default garbage collector, depending on the provided memory size.

Default Heap Size Is Always 1/4 of Available Memory! Really?

$ docker run -m 512m openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize     = 134217728     {product} {ergonomic}
     bool UseG1GC         = false         {product} {default}
     bool UseSerialGC     = true          {product} {ergonomic}
$ docker run -m 256m openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize     = 132120576      {product} {ergonomic}
     bool UseG1GC         = false          {product} {default}
     bool UseSerialGC     = true           {product} {ergonomic}


From a certain amount of memory, the JVM stops choosing 1/4 of the heap size and starts making the ratio smaller and smaller to provide your application bigger heap memory.

Why SerialGC?

$ docker run -m 1g openjdk:11-jre java -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize      = 268435456      {product} {ergonomic}
     bool UseG1GC          = false          {product} {default}
     bool UseSerialGC      = true           {product} {ergonomic}
$ docker run -m 1g openjdk:11-jre java -Xmx700m -XX:+PrintFlagsFinal -version | grep -E "(MAX|UseSerialGC|UseG1GC|MaxHeapSize)"
   size_t MaxHeapSize      = 734003200         {product} {command line}
     bool UseG1GC          = false             {product} {default}
     bool UseSerialGC      = true              {product} {ergonomic}


This is very weird, according to the example above, we should still have G1GC as a default, but know we can notice that JVM picked up SerialGC. One takeaway, you always have to try out what defaults were picked up by JVM and think if it's reasonable and suitable for you. You can notice that parameters defined by JVM are marked as ergonomic JVM options.

Thank you for reading my article and please take a look at the comments below. If you like being notified about new posts, then start following me on Twitter: @p_bouda.

Java (programming language) Java virtual machine Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • Ways To Reduce JVM Docker Image Size
  • Best Practices: Java Memory Arguments for Containers
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!