Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the JVM memory of the container environment

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to set up the JVM memory in the container environment". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to set up the JVM memory in the container environment" can help you solve your doubts.

Basic knowledge of Java Heap

By default, the heap size that jvm automatically allocates depends on the machine configuration, for example, we go to a 64 GB memory server:

Java-XX:+PrintFlagsFinal-version | grep-Ei "maxheapsize | maxram" uintx DefaultMaxRAMFraction = 4 {product} uintx MaxHeapSize: = 16875782144 {product} uint64_t MaxRAM = 137438953472 {pd product} uintx MaxRAMFraction = 4 {product} double MaxRAMPercentage = 25.000000 {product} java version "1.8.0mm 192" Java (TM) SE Runtime Environment (build 1.8.0_192-b12) Java HotSpot (TM) 64-Bit Server VM (build 25.192-b12 Mixed mode)

As you can see, the maximum MaxHeapSize allocated by JVM is 16G, and the calculation formula is as follows:

MaxHeapSize = MaxRAM * 1 / MaxRAMFraction

MaxRAMFraction defaults to 4, which means that a maximum of 25% of machine memory is used per jvm.

However, it is important to note that JVM actually uses more memory than heap memory:

JVM memory = heap memory + thread stack memory (XSS) * number of threads + startup overhead (constant overhead)

The default XSS is usually from 256KB to 1MB, which means that each thread allocates at least 256K additional memory, and constant overhead is the other memory allocated by JVM.

We can specify the maximum heap size with-Xmx.

Java-XX:+PrintFlagsFinal-Xmx1g-version | grep-Ei "maxheapsize | maxram" uintx DefaultMaxRAMFraction = 4 {product} uintx MaxHeapSize: = 1073741824 {product} uint64_t MaxRAM = 137438953472 {pd product} uintx MaxRAMFraction = 4 {product} double MaxRAMPercentage = 25.000000 {product} java version "1.8.0mm 192" Java (TM) SE Runtime Environment (build 1.8.0_192-b12) Java HotSpot (TM) 64-Bit Server VM (build 25.192-b12 Mixed mode)

In addition, you can use XX:MaxRAM to specify.

Java-XX:+PrintFlagsFinal-XX:MaxRAM=1g-version | grep-Ei

However, if you specify-Xmx or MaxRAM, you need to know the memory of the machine. A better way is to set MaxRAMFraction. Here is the ratio of available memory for different Fraction:

+-+ +

| | MaxRAMFraction |% of RAM for heap |

| |-+-|

| | 1 | 100% |

| | 2 | 50% |

| | 3 | 33% |

| | 4 | 25% |

+-+ +

Java Heap of container environment

In the container environment, because java cannot obtain the memory limit of the container, it can only obtain the configuration of the server:

$docker run-- rm alpine free-m total used free shared buffers cachedMem: 1998 1565 432 0 8 1244$ docker run-- rm-m 256m alpine free-m total used free shared buffers cachedMem: 1998 1552 445 1 8 1244

This can easily lead to unnecessary problems, such as limiting the container to use 100m of memory, but jvm allocates initialization memory according to the server configuration, causing the java process to exceed the container limit and be dropped by kill. To solve this problem, you can set-Xmx or MaxRAM to solve it, but as described in the first part, this is too inelegant!

To solve this problem, Java 10 introduces + UseContainerSupport (enabled by default), which allows JVM to allocate reasonable heap memory in the container environment. And, after the JDK8U191 version, this feature was introduced to JDK8, and JDK8 is the widely used version of JDK.

UseContainerSupport

-XX:+UseContainerSupport allows JVM to read cgroup restrictions, such as available CPU and RAM, from the host and configure them accordingly. In this way, when the container exceeds the memory limit, an OOM exception is thrown instead of killing the container.

This feature is available on Java 8u191 +, 10 and later.

Note that after version 191,-XX: {Min | Max} RAMFraction is deprecated and-XX:MaxRAMPercentage is introduced, with a value between 0.0 and 100.0, with a default value of 25.0.

Best practic

Pull the latest openjdk:8-jre-alpine as the base package. As of this blog, the latest version is 212, > 191.

Docker run-it-- rm openjdk:8-jre-alpine java-versionopenjdk version "1.8.0x212" OpenJDK Runtime Environment (IcedTea 3.12.0) (Alpine 8.212.04-r0) OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

We build a basic image. The dockerfile is as follows:

FROM openjdk:8-jre-alpineMAINTAINER jadepengRUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > / etc/apk/repositories\ & & echo" http://mirrors.aliyun.com/alpine/v3.6/community" > > / etc/apk/repositories\ & & apk update upgrade\ & & apk add-- no-cache procps unzip curl bash tzdata\ & & ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtime\ & & Echo "Asia/Shanghai" > / etc/timezoneRUN apk add-- update ttf-dejavu & & rm-rf / var/cache/apk/*

In the startup parameters of the application, set-XX:+UseContainerSupport, set-XX:MaxRAMPercentage=75.0, so as to leave enough memory space for other processes (debug, monitoring) without wasting RAM.

After reading this, the article "how to set up the JVM memory in the container environment" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report