Skip to content

Commit 91f0511

Browse files
authored
Merge pull request #320 from jmtd/OPENJDK-559-MaxRAMPercentage
[Openjdk 559] max ram percentage
2 parents 059275e + afc16d3 commit 91f0511

File tree

7 files changed

+94
-156
lines changed

7 files changed

+94
-156
lines changed

modules/jvm/api/module.yaml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@ 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: 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.
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"
1819
- name: JAVA_INITIAL_MEM_RATIO
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"
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"
2126
- name: JAVA_MAX_INITIAL_MEM
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"
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"
2433
- name: JAVA_DIAGNOSTICS
2534
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.**
2635
example: "true"
@@ -30,9 +39,6 @@ envs:
3039
- name: JAVA_DEBUG_PORT
3140
description: Port used for remote debugging. Defaults to *5005*.
3241
example: "8787"
33-
- name: CONTAINER_MAX_MEMORY
34-
description: Memory limit given to the container.
35-
example: "1024"
3642
- name: GC_MIN_HEAP_FREE_RATIO
3743
description: Minimum percentage of heap free after GC to avoid expansion.
3844
example: "20"

modules/jvm/bash/artifacts/opt/jboss/container/java/jvm/container-limits

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

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

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,6 @@
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-
358
# stubs for jvm specific overrides
369
jvm_specific_options() {
3710
:
@@ -46,63 +19,25 @@ if [ -f "${JBOSS_CONTAINER_OPENJDK_JDK_MODULE}/jvm-options" ]; then
4619
source "${JBOSS_CONTAINER_OPENJDK_JDK_MODULE}/jvm-options"
4720
fi
4821

49-
initialize_container_limits() {
50-
# we can't run without limits
51-
source ${JBOSS_CONTAINER_JAVA_JVM_MODULE}/container-limits
52-
}
53-
5422
# Check for memory options and calculate a sane default if not given
5523
max_memory() {
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
24+
# Check if explicitly disabled
6525
if [ "x$JAVA_MAX_MEM_RATIO" = "x0" ]; then
6626
return
6727
fi
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
28+
echo "-XX:MaxRAMPercentage=${JAVA_MAX_MEM_RATIO:-80.0}"
7729
}
7830

7931
# Check for memory options and calculate a sane default if not given
8032
initial_memory() {
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"
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}"
10641
fi
10742
fi
10843
}
@@ -157,7 +92,5 @@ error_handling() {
15792
echo "-XX:+ExitOnOutOfMemoryError"
15893
}
15994

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

modules/maven/default/artifacts/opt/jboss/container/maven/default/maven.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function maven_init_var_MAVEN_SETTINGS_XML() {
4141
}
4242

4343
function maven_init_var_MAVEN_OPTS() {
44-
export MAVEN_OPTS="${MAVEN_OPTS:-$(${JBOSS_CONTAINER_JAVA_JVM_MODULE}/java-default-options)}"
44+
export MAVEN_OPTS="${MAVEN_OPTS:-$(${JBOSS_CONTAINER_JAVA_JVM_MODULE}/java-default-options) -XX:MaxRAMPercentage=25.0}"
4545
}
4646

4747
function maven_init_var_MAVEN_ARGS() {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ 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-
10095
# JAVA_LIB_DIR defaults to JAVA_APP_DIR
10196
export JAVA_LIB_DIR="${JAVA_LIB_DIR:-${JAVA_APP_DIR}}"
10297
if [ -z "${JAVA_MAIN_CLASS}" ] && [ -z "${JAVA_APP_JAR}" ]; then

tests/features/java/gc.feature

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,65 @@
1+
12
@ubi8/openjdk-8
23
@ubi8/openjdk-11
34
@ubi8/openjdk-17
45
Feature: Openshift OpenJDK GC tests
56

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

1114
Scenario: Check GC_MIN_HEAP_FREE_RATIO GC configuration
12-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
15+
Given container is started with env
1316
| variable | value |
1417
| GC_MIN_HEAP_FREE_RATIO | 5 |
15-
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
16-
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90
18+
Then container log should contain -XX:MinHeapFreeRatio=5
1719

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

2526
Scenario: Check GC_TIME_RATIO GC configuration
26-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
27+
Given container is started with env
2728
| variable | value |
2829
| GC_TIME_RATIO | 5 |
29-
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=5 -XX:AdaptiveSizePolicyWeight=90
30-
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=5 -XX:AdaptiveSizePolicyWeight=90
30+
Then container log should contain -XX:GCTimeRatio=5
3131

3232
Scenario: Check GC_ADAPTIVE_SIZE_POLICY_WEIGHT GC configuration
33-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
33+
Given container is started with env
3434
| variable | value |
3535
| GC_ADAPTIVE_SIZE_POLICY_WEIGHT | 80 |
36-
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=80
37-
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=80
36+
Then container log should contain -XX:AdaptiveSizePolicyWeight=80
3837

3938
Scenario: Check GC_MAX_METASPACE_SIZE GC configuration
40-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
39+
Given container is started with env
4140
| variable | value |
4241
| GC_MAX_METASPACE_SIZE | 120 |
43-
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
44-
And container log should contain -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=120m
42+
Then container log should contain -XX:MaxMetaspaceSize=120m
4543

4644
Scenario: Check GC_CONTAINER_OPTIONS configuration
47-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
45+
Given container is started with env
4846
| variable | value |
4947
| GC_CONTAINER_OPTIONS | -XX:+UseG1GC |
50-
Then s2i build log should contain Using MAVEN_OPTS -XX:+UseG1GC
51-
And container log should contain -XX:+UseG1GC
48+
Then container log should contain -XX:+UseG1GC
5249
And container log should not contain -XX:+UseParallelGC
5350

5451
Scenario: Check GC_METASPACE_SIZE GC configuration
55-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
56-
| variable | value |
52+
Given container is started with env
53+
| variable | value |
5754
| GC_METASPACE_SIZE | 120 |
58-
Then s2i build log should contain -XX:MetaspaceSize=120m
59-
And container log should contain -XX:MetaspaceSize=120m
55+
Then container log should contain -XX:MetaspaceSize=120m
6056
And container log should not contain integer expression expected
6157

6258
Scenario: Check GC_METASPACE_SIZE constrained by GC_MAX_METASPACE_SIZE GC configuration
63-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
64-
| variable | value |
65-
| GC_METASPACE_SIZE | 120 |
59+
Given container is started with env
60+
| variable | value |
61+
| GC_METASPACE_SIZE | 120 |
6662
| GC_MAX_METASPACE_SIZE | 90 |
67-
Then s2i build log should contain -XX:MaxMetaspaceSize=90m -XX:MetaspaceSize=90m
68-
And container log should contain -XX:MaxMetaspaceSize=90m -XX:MetaspaceSize=90m
63+
Then container log should contain -XX:MaxMetaspaceSize=90m
64+
And container log should contain -XX:MetaspaceSize=90m
6965

tests/features/java/memory.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Feature: OPENJDK-559 JVM Memory tests
2+
3+
@ubi8
4+
Scenario: Check default JVM max heap configuration
5+
Given container is started as uid 1000
6+
Then container log should contain -XX:MaxRAMPercentage=80.0
7+
8+
@ubi8
9+
Scenario: Check configured JVM max heap configuration
10+
Given container is started with env
11+
| variable | value |
12+
| JAVA_MAX_MEM_RATIO | 90.0 |
13+
Then container log should contain -XX:MaxRAMPercentage=90.0
14+
15+
@ubi8
16+
Scenario: Check default JVM initial heap configuration is unspecified
17+
Given container is started as uid 1000
18+
Then container log should not contain -XX:InitialRAMPercentage
19+
And container log should not contain -Xms
20+
21+
@ubi8
22+
Scenario: Check configured JVM max heap configuration
23+
Given container is started with env
24+
| variable | value |
25+
| JAVA_INITIAL_MEM_RATIO | 25.0 |
26+
Then container log should contain -XX:InitialRAMPercentage=25.0
27+
28+
@ubi8
29+
Scenario: check JAVA_MAX_INITIAL_MEM overrides JAVA_INITIAL_MEM_RATIO
30+
Given container is started with env
31+
| variable | value |
32+
| JAVA_INITIAL_MEM_RATIO | 25.0 |
33+
| JAVA_MAX_INITIAL_MEM | 4096m |
34+
Then container log should contain -Xms4096m
35+
And container log should not contain -XX:InitialRAMPercentage=25.0
36+
37+
# Not the runtime images
38+
@ubi8/openjdk-8
39+
@ubi8/openjdk-11
40+
@ubi8/openjdk-17
41+
Scenario: Ensure Maven doesn't use MaxRAMPercentage=80
42+
Given s2i build https://github.com/jboss-openshift/openshift-examples from spring-boot-sample-simple
43+
Then s2i build log should match regex INFO Using MAVEN_OPTS.*-XX:MaxRAMPercentage=25.0$

0 commit comments

Comments
 (0)