Skip to content

Commit 4776cfe

Browse files
authored
Merge pull request #313 from jmtd/revert-OPENJDK-559
Revert "Merge pull request #285 from jmtd/OPENJDK-559-MaxRAMPercentage"
2 parents 1b3a88f + 22f44fb commit 4776cfe

File tree

6 files changed

+158
-83
lines changed

6 files changed

+158
-83
lines changed

modules/jvm/api/module.yaml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,13 @@ envs:
1414
description: User specified Java options to be appended to generated options in JAVA_OPTS.
1515
example: "-Dsome.property=foo"
1616
- name: JAVA_MAX_MEM_RATIO
17-
description: Specify the maximum heap memory. Corresponds to the JVM argument `-XX:MaxRAMPercentage`. The default is `80.0` which means 80% of the available memory. You can disable this mechanism by setting the value to `0`.
18-
example: "90.0"
17+
description: Is used when no `-Xmx` option is given in **JAVA_OPTS**. This is used to calculate a default maximal heap memory based on a containers restriction. If used in a container without any memory constraints for the container then this option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio of the container available memory as set here. The default is `50` which means 50% of the available memory is used as an upper boundary. You can skip this mechanism by setting this value to `0` in which case no `-Xmx` option is added.
1918
- name: JAVA_INITIAL_MEM_RATIO
20-
description:
21-
Specify the initial heap memory. Corresponds to the JVM argument
22-
`-XX:InitialRAMPercentage`. By default this is not specified.
23-
**This is deprecated and will be removed in a future release. Users should
24-
specify `-XX:InitialRAMPercentage` directly in JAVA_OPTS instead.**
25-
example: "25.0"
19+
description: Is used when no `-Xms` option is given in **JAVA_OPTS**. This is used to calculate a default initial heap memory based on the maximum heap memory. If used in a container without any memory constraints for the container then this option has no effect. If there is a memory constraint then `-Xms` is set to a ratio of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx` is used as the initial heap size. You can skip this mechanism by setting this value to `0` in which case no `-Xms` option is added.
20+
example: "25"
2621
- name: JAVA_MAX_INITIAL_MEM
27-
description:
28-
This value is passed through to the `-Xms` Java option, setting both the
29-
minimum and initial heap size. By default this is unset.
30-
**This is deprecated and will be removed in a future release. Users should
31-
specify `-Xms` directly in JAVA_OPTS instead.**
32-
example: "4096m"
22+
description: Is used when no `-Xms` option is given in **JAVA_OPTS**. This is used to calculate the maximum value of the initial heap memory. If used in a container without any memory constraints for the container then this option has no effect. If there is a memory constraint then `-Xms` is limited to the value set here. The default is 4096MB which means the calculated value of `-Xms` never will be greater than 4096MB. The value of this variable is expressed in MB.
23+
example: "4096"
3324
- name: JAVA_DIAGNOSTICS
3425
description: Set this to get some diagnostics information to standard output when things are happening. **Note:** This option, if set to true, will set `-XX:+UnlockDiagnosticVMOptions`. **Disabled by default.**
3526
example: "true"
@@ -39,6 +30,9 @@ envs:
3930
- name: JAVA_DEBUG_PORT
4031
description: Port used for remote debugging. Defaults to *5005*.
4132
example: "8787"
33+
- name: CONTAINER_MAX_MEMORY
34+
description: Memory limit given to the container.
35+
example: "1024"
4236
- name: GC_MIN_HEAP_FREE_RATIO
4337
description: Minimum percentage of heap free after GC to avoid expansion.
4438
example: "20"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
# Detected container limits
4+
# If found these are exposed as the following environment variables:
5+
#
6+
# - CONTAINER_MAX_MEMORY
7+
# - CONTAINER_CORE_LIMIT
8+
#
9+
# This script is meant to be sourced.
10+
11+
max_unbounded() {
12+
cat /proc/meminfo | grep 'MemTotal:' | awk '{print $2*1024}'
13+
}
14+
15+
container_memory() {
16+
# High number which is the max limit unit which memory is supposed to be
17+
# unbounded.
18+
local mem_file="/sys/fs/cgroup/memory/memory.limit_in_bytes"
19+
local max_mem_unbounded="$(max_unbounded)"
20+
if [ -r "${mem_file}" ]; then
21+
local max_mem="$(cat ${mem_file})"
22+
if [ ${max_mem} -lt ${max_mem_unbounded} ]; then
23+
echo "${max_mem}"
24+
fi
25+
fi
26+
}
27+
28+
min() {
29+
printf "%s\n" "$@" | sort -g | head -n1
30+
}
31+
32+
local max_mem="$(container_memory)"
33+
if [ x$max_mem != x ]; then
34+
export CONTAINER_MAX_MEMORY="$max_mem"
35+
fi

modules/jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@
55
#
66
# Usage: JAVA_OPTS="$(java-default-options.sh)"
77

8+
# Env Vars respected:
9+
10+
# JAVA_OPTIONS: Checked for already set options. Deprecated, use JAVA_OPTS.
11+
# JAVA_OPTS: Checked for already set options
12+
# JAVA_INITIAL_MEM_RATIO: Ratio of maximum memory to use for initial heap size
13+
# (i.e. -Xms). Defaults to 25 (i.e -Xms=-Xmx/4).
14+
# JAVA_MAX_INITIAL_MEM: The maximum value of the initial heap size, defaults to 4G.
15+
# JAVA_MAX_MEM_RATIO: Ratio use to calculate a default maximum Memory, in percent.
16+
# E.g. the default value "50" implies that 50% of the Memory
17+
# given to the container is used as the maximum heap memory with
18+
# '-Xmx'. It is a heuristic and should be better backed up with real
19+
# experiments and measurements.
20+
# For a good overviews what tuning options are available -->
21+
# https://youtu.be/Vt4G-pHXfs4
22+
# https://www.youtube.com/watch?v=w1rZOY5gbvk
23+
# https://vimeo.com/album/4133413/video/181900266
24+
# Also note that heap is only a small portion of the memory used by a JVM. There are lot
25+
# of other memory areas (metadata, thread, code cache, ...) which addes to the overall
26+
# size. There is no easy solution for this, 50% seems to be are reasonable compromise.
27+
# However, when your container gets killed because of an OOM, then you should tune
28+
# the absolute values
29+
#
30+
31+
__DEFAULT_JAVA_MAX_MEM_RATIO=50
32+
__DEFAULT_JAVA_INITIAL_MEM_RATIO=25
33+
__DEFAULT_JAVA_MAX_INITIAL_MEM=4096
34+
835
# stubs for jvm specific overrides
936
jvm_specific_options() {
1037
:
@@ -19,25 +46,63 @@ if [ -f "${JBOSS_CONTAINER_OPENJDK_JDK_MODULE}/jvm-options" ]; then
1946
source "${JBOSS_CONTAINER_OPENJDK_JDK_MODULE}/jvm-options"
2047
fi
2148

49+
initialize_container_limits() {
50+
# we can't run without limits
51+
source ${JBOSS_CONTAINER_JAVA_JVM_MODULE}/container-limits
52+
}
53+
2254
# Check for memory options and calculate a sane default if not given
2355
max_memory() {
24-
# Check if explicitly disabled
56+
# Check whether -Xmx is already given in JAVA_OPTIONS. Then we dont
57+
# do anything here
58+
# XXX: I think this should be removed. If folks want to hard code max/min,
59+
# then they can set the ratios to zero and set the options in JAVA_OPTS_APPEND.
60+
if echo "${JAVA_OPTS:-${JAVA_OPTIONS}}" | grep -q -- "-Xmx"; then
61+
return
62+
fi
63+
64+
# Check if explicitely disabled
2565
if [ "x$JAVA_MAX_MEM_RATIO" = "x0" ]; then
2666
return
2767
fi
28-
echo "-XX:MaxRAMPercentage=${JAVA_MAX_MEM_RATIO:-80.0}"
68+
69+
# Check for the 'real memory size' and caluclate mx from a ratio
70+
# given (default is 50%)
71+
if [ "x$CONTAINER_MAX_MEMORY" != x ]; then
72+
local max_mem="${CONTAINER_MAX_MEMORY}"
73+
local ratio=${JAVA_MAX_MEM_RATIO:-${__DEFAULT_JAVA_MAX_MEM_RATIO}}
74+
local mx=$(echo "${max_mem} ${ratio} 1048576" | awk '{printf "%d\n" , ($1*$2)/(100*$3) + 0.5}')
75+
echo "-Xmx${mx}m"
76+
fi
2977
}
3078

3179
# Check for memory options and calculate a sane default if not given
3280
initial_memory() {
33-
# JAVA_MAX_INITIAL_MEM is deprecated and will be removed in a future release
34-
if [ -n "$JAVA_MAX_INITIAL_MEM" ]; then
35-
echo "-Xms$JAVA_MAX_INITIAL_MEM"
36-
else
37-
# there's no point setting this if we are passing -Xms, since -Xms
38-
# overrides this
39-
if [ -n "$JAVA_INITIAL_MEM_RATIO" ]; then
40-
echo "-XX:InitialRAMPercentage=${JAVA_INITIAL_MEM_RATIO}"
81+
# Check whether -Xms is already given in JAVA_OPTS. Then we dont
82+
# do anything here
83+
# XXX: I think this should be removed. If folks want to hard code max/min,
84+
# then they can set the ratios to zero and set the options in JAVA_OPTS_APPEND.
85+
if echo "${JAVA_OPTS:-${JAVA_OPTIONS}}" | grep -q -- "-Xms"; then
86+
return
87+
fi
88+
89+
# Check if explicitly disabled
90+
if [ "x$JAVA_INITIAL_MEM_RATIO" = "x0" ]; then
91+
return
92+
fi
93+
94+
# Check for the 'real memory size' and calculate ms from a ratio
95+
# given (default is 25%)
96+
if [ "x$CONTAINER_MAX_MEMORY" != x ]; then
97+
local max_mem="${CONTAINER_MAX_MEMORY}"
98+
local max_ratio=${JAVA_MAX_MEM_RATIO:-${__DEFAULT_JAVA_MAX_MEM_RATIO}}
99+
local initial_ratio=${JAVA_INITIAL_MEM_RATIO:-${__DEFAULT_JAVA_INITIAL_MEM_RATIO}}
100+
local ms=$(echo "${max_mem} ${max_ratio} ${initial_ratio} 1048576" | awk '{printf "%d\n" , ($1*(($2*$3)/10000))/$4 + 0.5}')
101+
local max_initial_memory=${JAVA_MAX_INITIAL_MEM:-${__DEFAULT_JAVA_MAX_INITIAL_MEM}}
102+
if [ "${ms}" -lt "${max_initial_memory}" ] ; then
103+
echo "-Xms${ms}m"
104+
else
105+
echo "-Xms${max_initial_memory}m"
41106
fi
42107
fi
43108
}
@@ -92,5 +157,7 @@ error_handling() {
92157
echo "-XX:+ExitOnOutOfMemoryError"
93158
}
94159

160+
initialize_container_limits > /dev/null
161+
95162
## Echo options, trimming trailing and multiple spaces
96163
echo "$(initial_memory) $(max_memory) $(gc_config) $(diagnostics) $(error_handling)" | awk '$1=$1'

modules/run/artifacts/opt/jboss/container/java/run/run-java.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ load_env() {
9292
fi
9393
export JAVA_APP_DIR
9494

95+
# Read in container limits and export the as environment variables
96+
if [ -f "${JBOSS_CONTAINER_JAVA_JVM_MODULE}/container-limits" ]; then
97+
source "${JBOSS_CONTAINER_JAVA_JVM_MODULE}/container-limits"
98+
fi
99+
95100
# JAVA_LIB_DIR defaults to JAVA_APP_DIR
96101
export JAVA_LIB_DIR="${JAVA_LIB_DIR:-${JAVA_APP_DIR}}"
97102
if [ -z "${JAVA_MAIN_CLASS}" ] && [ -z "${JAVA_APP_JAR}" ]; then

tests/features/java/gc.feature

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,71 @@
11
@openjdk
2+
@ubi8/openjdk-8
3+
@ubi8/openjdk-11
4+
@ubi8/openjdk-17
25
@redhat-openjdk-18
3-
@ubi8
46
Feature: Openshift OpenJDK GC tests
57

68
Scenario: Check default GC configuration
7-
Given container is started as uid 1000
8-
Then container log should contain -XX:+UseParallelGC
9-
And container log should contain -XX:MinHeapFreeRatio=10
10-
And container log should contain -XX:MaxHeapFreeRatio=20
11-
And container log should contain -XX:GCTimeRatio=4
9+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
10+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
11+
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
1212

1313
Scenario: Check GC_MIN_HEAP_FREE_RATIO GC configuration
14-
Given container is started with env
14+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
1515
| variable | value |
1616
| GC_MIN_HEAP_FREE_RATIO | 5 |
17-
Then container log should contain -XX:MinHeapFreeRatio=5
17+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
18+
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
1819

1920
Scenario: Check GC_MAX_HEAP_FREE_RATIO GC configuration
20-
Given container is started with env
21+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
2122
| variable | value |
2223
| GC_MAX_HEAP_FREE_RATIO | 50 |
23-
Then container log should contain -XX:MaxHeapFreeRatio=50
24+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
25+
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
2426

2527
Scenario: Check GC_TIME_RATIO GC configuration
26-
Given container is started with env
28+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
2729
| variable | value |
2830
| GC_TIME_RATIO | 5 |
29-
Then container log should contain -XX:GCTimeRatio=5
31+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=5 -XX:AdaptiveSizePolicyWeight=90
32+
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=5 -XX:AdaptiveSizePolicyWeight=90
3033

3134
Scenario: Check GC_ADAPTIVE_SIZE_POLICY_WEIGHT GC configuration
32-
Given container is started with env
35+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
3336
| variable | value |
3437
| GC_ADAPTIVE_SIZE_POLICY_WEIGHT | 80 |
35-
Then container log should contain -XX:AdaptiveSizePolicyWeight=80
38+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=80
39+
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=80
3640

3741
Scenario: Check GC_MAX_METASPACE_SIZE GC configuration
38-
Given container is started with env
42+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
3943
| variable | value |
4044
| GC_MAX_METASPACE_SIZE | 120 |
41-
Then container log should contain -XX:MaxMetaspaceSize=120m
45+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=120m
46+
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=120m
4247

4348
Scenario: Check GC_CONTAINER_OPTIONS configuration
44-
Given container is started with env
49+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
4550
| variable | value |
4651
| GC_CONTAINER_OPTIONS | -XX:+UseG1GC |
47-
Then container log should contain -XX:+UseG1GC
52+
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseG1GC
53+
And container log should contain -XX:+UseG1GC
4854
And container log should not contain -XX:+UseParallelGC
4955

5056
Scenario: Check GC_METASPACE_SIZE GC configuration
51-
Given container is started with env
52-
| variable | value |
57+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
58+
| variable | value |
5359
| GC_METASPACE_SIZE | 120 |
54-
Then container log should contain -XX:MetaspaceSize=120m
60+
Then s2i build log should contain -XX:MetaspaceSize=120m
61+
And container log should contain -XX:MetaspaceSize=120m
5562
And container log should not contain integer expression expected
5663

5764
Scenario: Check GC_METASPACE_SIZE constrained by GC_MAX_METASPACE_SIZE GC configuration
58-
Given container is started with env
59-
| variable | value |
60-
| GC_METASPACE_SIZE | 120 |
65+
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
66+
| variable | value |
67+
| GC_METASPACE_SIZE | 120 |
6168
| GC_MAX_METASPACE_SIZE | 90 |
62-
Then container log should contain -XX:MaxMetaspaceSize=90m
63-
And container log should contain -XX:MetaspaceSize=90m
69+
Then s2i build log should contain -XX:MaxMetaspaceSize=90m -XX:MetaspaceSize=90m
70+
And container log should contain -XX:MaxMetaspaceSize=90m -XX:MetaspaceSize=90m
6471

tests/features/java/memory.feature

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)