Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

OpenJDK and Containers

April 4, 2017
Christine Flood
Related topics:
ContainersDeveloper toolsJava
Related products:
Red Hat build of OpenJDK

    What can be done to help the OpenJDK JVM play well in the world of Linux Containers?
    I thought I'd start tackling this issue by answering some frequently asked questions:

    Why is it when I specify -Xmx=1g my JVM uses up more memory than 1gb of memory?

    Specifying -Xmx=1g is telling the JVM to allocate a 1gb heap. It's not telling the JVM to limit its entire memory usage to 1gb. There are card tables, code caches, and all sorts of other off heap data structures. The parameter you use to specify total memory usage is -XX:MaxRAM. Be aware that with -XX:MaxRam=500m your heap will be approximately 250mb.

    Why is it when I specify -m 10m to my Linux container the JVM appears to ignore the limit?

    The JVM historically looked in /proc to figure out how much memory was available and then set its heap size based on that value. Unfortunately, containers like Docker don't provide container specific information in /proc. I've proposed a patch which has been accepted upstream to provide a -XX:+UseCGroupMemoryLimitForHeap command line argument which tells the JVM to look in /sys/fs/cgroup/memory/memory.limit_in_bytes to figure out how much memory is available. If this patch isn't available in the OpenJDK version you are running you can simulate it by setting -XX:MaxRAM=n explicitly.

    What if I specify cpusets?

    There is a patch in OpenJDK8, which will use the information available to the cgroup to calculate the appropriate number of parallel GC threads. However, if this patch is not available in your version of OpenJDK you may end up with 8 parallel GC threads and only 2 cpus in your container. The workaround is to specify the number of parallel gc threads explicitly. -XX:ParallelGCThreads=2.  If you only have 1 CPU in your container I highly recommend that you run with -XX:+UseSerialGC and avoid parallel GC altogether.

    OK, so I know I can explicitly set things like heap size and parallel gc threads, but how can I tell the JVM I don't care about pause time or throughput I just want it to use as few resources as possible?

    -XX:+UseSerialGC will run with only 1 garbage collection thread and will run with the smallest heap overhead.
    -XX:+TieredCompilation -XX:TieredStopAtLevel=1 will disable the optimizing compiler and save some space.

    My program has a startup phase where it needs a lot of heap but will settle into a quiet looping phase where it doesn't need as much. Can I configure the heap to grow, shrink, and give memory it isn't currently using back to the operating system?

    SerialGC will do this for you, but you can ask it to be more aggressive.
    -XX:MinHeapFreeRatio=20 (This defaults to Grow when the heap is greater than 80% occupied)
    -XX:MaxHeapFreeRatio=40 (Shrink when the heap is less than 60% occupied).
    Parallel GC will do this for you as well. We recommend the following additional parameters:
    -XX:GCTimeRatio=4
    -XX:AdaptiveSizePolicyWeight=90

    OK, I have a 2gb heap, and my jvm instance is using 4gb, where are the other 2gb going?

    You can track native memory by running Java with the following command line arguments:

    java -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking="summary" -XX:+PrintNMTStatistics

    You can decrease some of these parameters.  If you are running with many threads, for example, decreasing the size of your Java thread stacks might help.  -Xss228k will decrease the size of your Java stacks.

    JVM Argument Effect
    -XX:+UseSerialGC Uses only 1 GC thread. This both limits the cpu instruction count for garbage collection, but also results in the minimal memory footprint.
    -XX:MaxRAM=n Sets the maximum amount of memory used by the JVM to n, where n may be expressed in terms of megabytes 100m or gigabytes 2g.
    -XX:+UseCGroupMemoryLimitForHeap This flag present in the more recent builds of JDK8 tells the JVM to use the information in /sys/fs/cgroup/memory/memory.limit_in_bytes to calculate memory defaults.
    -XX:ParallelGCThreads=n Set the number of parallel GC threads to n. This is helpful if you are trying to limit the cpu usage of your container, or if you are running with a JVM that doesn't include the patch to calculate GC threads based on processors available to the cgroup.
    -XX:+TieredCompilation
    -XX:TieredStopAtLevel=1
    Turns off the optimizing compiler. This can sometimes decrease the footprint of your running JVM.
    -XX:MinHeapFreeRatio=20
    -XX:MaxHeapFreeRatio=40
    These parameters tell the heap to shrink aggressively and to grow conservatively.  Thereby optimizing the amount of memory available to the operating system.
    -XX:GCTimeRatio=4
    -XX:AdaptiveSizePolicyWeight=90
    These parameters are necessary when running parallel GC if you want to use the Min and Max Heap Free ratios.
    -XX:+UnlockDiagnosticVMOptions
    -XX:NativeMemoryTracking=
    "summary"
    -XX:+PrintNMTStatistics
    These options will print out the non-heap memory usage of your JVM.
    -Xss228k This will decrease the size of your Java Stacks.

    If you have any additional questions, please drop me an email: chf@redhat.com.


    Download the OpenJDK and accept the terms and conditions of the Red Hat Developer Program, which provides no-cost subscriptions for development use only.

    Last updated: April 22, 2022

    Recent Posts

    • A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    • Configure a split disk on OpenShift Container Platform

    • Red Hat Enterprise Linux 10.2 and 9.8: Top features for developers

    • What GPU kernels mean for your distributed inference

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.