Skip to content

Commit 25c7d28

Browse files
authored
Merge pull request #285 from jmtd/OPENJDK-559-MaxRAMPercentage
[OPENJDK-559] Set MaxRAMPercentage=80 by default
2 parents 4d34406 + b6beb27 commit 25c7d28

File tree

6 files changed

+83
-158
lines changed

6 files changed

+83
-158
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/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 & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,64 @@
11
@openjdk
2-
@ubi8/openjdk-8
3-
@ubi8/openjdk-11
4-
@ubi8/openjdk-17
52
@redhat-openjdk-18
3+
@ubi8
64
Feature: Openshift OpenJDK GC tests
75

86
Scenario: Check default GC configuration
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
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
1212

1313
Scenario: Check GC_MIN_HEAP_FREE_RATIO GC configuration
14-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
14+
Given container is started with env
1515
| variable | value |
1616
| GC_MIN_HEAP_FREE_RATIO | 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
17+
Then container log should contain -XX:MinHeapFreeRatio=5
1918

2019
Scenario: Check GC_MAX_HEAP_FREE_RATIO GC configuration
21-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
20+
Given container is started with env
2221
| variable | value |
2322
| GC_MAX_HEAP_FREE_RATIO | 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
23+
Then container log should contain -XX:MaxHeapFreeRatio=50
2624

2725
Scenario: Check GC_TIME_RATIO GC configuration
28-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
26+
Given container is started with env
2927
| variable | value |
3028
| GC_TIME_RATIO | 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
29+
Then container log should contain -XX:GCTimeRatio=5
3330

3431
Scenario: Check GC_ADAPTIVE_SIZE_POLICY_WEIGHT GC configuration
35-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
32+
Given container is started with env
3633
| variable | value |
3734
| GC_ADAPTIVE_SIZE_POLICY_WEIGHT | 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
35+
Then container log should contain -XX:AdaptiveSizePolicyWeight=80
4036

4137
Scenario: Check GC_MAX_METASPACE_SIZE GC configuration
42-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
38+
Given container is started with env
4339
| variable | value |
4440
| GC_MAX_METASPACE_SIZE | 120 |
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
41+
Then container log should contain -XX:MaxMetaspaceSize=120m
4742

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

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

6457
Scenario: Check GC_METASPACE_SIZE constrained by GC_MAX_METASPACE_SIZE GC configuration
65-
Given s2i build https://github.com/jboss-openshift/openshift-quickstarts from undertow-servlet
66-
| variable | value |
67-
| GC_METASPACE_SIZE | 120 |
58+
Given container is started with env
59+
| variable | value |
60+
| GC_METASPACE_SIZE | 120 |
6861
| GC_MAX_METASPACE_SIZE | 90 |
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
62+
Then container log should contain -XX:MaxMetaspaceSize=90m
63+
And container log should contain -XX:MetaspaceSize=90m
7164

tests/features/java/memory.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@openjdk
2+
@redhat-openjdk-18
3+
@ubi8
4+
Feature: OPENJDK-559 JVM Memory tests
5+
6+
Scenario: Check default JVM max heap configuration
7+
Given container is started as uid 1000
8+
Then container log should contain -XX:MaxRAMPercentage=80.0
9+
10+
Scenario: Check configured JVM max heap configuration
11+
Given container is started with env
12+
| variable | value |
13+
| JAVA_MAX_MEM_RATIO | 90.0 |
14+
Then container log should contain -XX:MaxRAMPercentage=90.0
15+
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+
Scenario: Check configured JVM max heap configuration
22+
Given container is started with env
23+
| variable | value |
24+
| JAVA_INITIAL_MEM_RATIO | 25.0 |
25+
Then container log should contain -XX:InitialRAMPercentage=25.0
26+
27+
Scenario: check JAVA_MAX_INITIAL_MEM overrides JAVA_INITIAL_MEM_RATIO
28+
Given container is started with env
29+
| variable | value |
30+
| JAVA_INITIAL_MEM_RATIO | 25.0 |
31+
| JAVA_MAX_INITIAL_MEM | 4096m |
32+
Then container log should contain -Xms4096m
33+
And container log should not contain -XX:InitialRAMPercentage=25.0

0 commit comments

Comments
 (0)