Java

容器環境中的Java大堆

  • October 18, 2021

我正在嘗試在 kubernetes 上執行一個 Jetty 網路伺服器,它在我們的生產環境中需要大量的堆 ~ 250 GB,在我們的測試環境中 ~ 50 GB。

我正在使用jetty:9.4-jdk11,我試圖避免顯式設置XmsXmx標誌,因為不同環境之間的值不同,因為我認為依賴-XX:MaxRAMPercentage -XX:InitialRAMPercentage會更好,但無論我嘗試什麼,我都無法MaxHeapSize通過32178700288 ~ 30 GB。

Node 上只有 Java 應用程序和一些小 sidcar,有 64 GB 記憶體。

Dockerfile

FROM jetty:9.4-jdk11

ENV APP_WAR root.war
ENV APP_EXPLODED_WAR root/
ENV APP_DESTINATION_PATH $JETTY_BASE/webapps/
ENV APP_DESTINATION_WAR $APP_DESTINATION_PATH$APP_WAR
ENV APP_DESTINATION_EXPLODED_WAR $APP_DESTINATION_PATH$APP_EXPLODED_WAR

ADD . $APP_DESTINATION_EXPLODED_WAR

ENV JAVA_OPTIONS -XX:+PrintFlagsFinal -XX:MaxRAMPercentage=90 -XX:InitialRAMPercentage=90 -XX:-OmitStackTraceInFastThrow -XX:+UseStringDeduplication -Xlog:gc*,stringdedup*=debug:file=/tmp/gc.log:time

容器資源設置

resources:
 limits:
   cpu: "8"
   memory: 60G
 requests:
   cpu: "6"
   memory: 60G

基於這些值,我應該得到 60 GB MaxHeapSize~ 54 GB 的 90%,而不是 30 GB。知道我缺少什麼嗎?

-XX:+UseCompressedOopsJava 11 上預設啟用的JVM 參數是原因,來自文件

-XX:-UseCompressedOops
   
Disables the use of compressed pointers. By default, this option is enabled, and compressed pointers are used when Java heap sizes are less than 32 GB. When this option is enabled, object references are represented as 32-bit offsets instead of 64-bit pointers, which typically increases performance when running the application with Java heap sizes of less than 32 GB. This option works only for 64-bit JVMs.
   
It’s also possible to use compressed pointers when Java heap sizes are greater than 32 GB. See the -XX:ObjectAlignmentInBytes option.

只需更改-XX:+-XX:-如上所示將禁用它。

-XX:ObjectAlignmentInBytes在我的情況下也不建議使用相同的文件

-XX:ObjectAlignmentInBytes=alignment

Sets the memory alignment of Java objects (in bytes). By default, the value is set to 8 bytes. The specified value should be a power of 2, and must be within the range of 8 and 256 (inclusive). This option makes it possible to use compressed pointers with large Java heap sizes.

The heap size limit in bytes is calculated as:

4GB * ObjectAlignmentInBytes

Note:

As the alignment value increases, the unused space between objects also increases. As a result, you may not realize any benefits from using compressed pointers with large Java heap sizes.

但我想它值得測試。

引用自:https://serverfault.com/questions/1080535