diff --git a/docs/configuration/jvm-options.md b/docs/configuration/jvm-options.md index fd62e288..e16150bd 100644 --- a/docs/configuration/jvm-options.md +++ b/docs/configuration/jvm-options.md @@ -10,6 +10,12 @@ By default, the image declares an initial and maximum Java memory-heap limit of The values of all three are passed directly to the JVM and support format/units as `[g|G|m|M|k|K]`. +To have control over heap size, without relying on absolute memory sizes percentages are also supported using `%`. + +!!! info "RAMPercentage parameters" + Percentage based heap sizing is enabled using `-XX:InitialRAMPercentage` for `INIT_MEMORY` and `-XX:MaxRAMPercentage` for `MAX_MEMORY`. + For details on the function of these parameters look [here](https://www.baeldung.com/java-jvm-parameters-rampercentage). + !!! example "Using docker run" ``` @@ -37,19 +43,7 @@ The values of all three are passed directly to the JVM and support format/units MAX_MEMORY: 4G ``` -To let the JVM calculate the heap size from the container declared memory limit, unset `MEMORY` with an empty value, such as `-e MEMORY=""`. By default, the JVM will use 25% of the container memory limit as the heap limit; however, as an example the following would tell the JVM to use 75% of the container limit of 4GB of memory: - -!!! example "MaxRAMPercentage using compose file" - - ``` - environment: - MEMORY: "" - JVM_XX_OPTS: "-XX:MaxRAMPercentage=75" - deploy: - resources: - limits: - memory: 4G - ``` +To let the JVM calculate the heap size from the container declared memory limit, unset `MEMORY` with an empty value, such as `-e MEMORY=""`. By default, the JVM will use 25% of the container memory limit as the heap limit. !!! important The settings above only set the Java **heap** limits. Memory resource requests and limits on the overall container should also account for non-heap memory usage. An extra 25% is [a general best practice](https://dzone.com/articles/best-practices-java-memory-arguments-for-container). diff --git a/scripts/start-deployBukkitSpigot b/scripts/start-deployBukkitSpigot index f1120d6c..1b8c83eb 100755 --- a/scripts/start-deployBukkitSpigot +++ b/scripts/start-deployBukkitSpigot @@ -28,7 +28,19 @@ function buildSpigotFromSource { mkdir ${tempDir} cd ${tempDir} - jvmOpts="-Xms${INIT_MEMORY:-$MEMORY} -Xmx${MAX_MEMORY:-$MEMORY}" + jvmOpts="" + + if isPercentage "${INIT_MEMORY:-$MEMORY}"; then + jvmOpts+="-XX:InitialRAMPercentage=$(getPercentageValue "${INIT_MEMORY:-$MEMORY}") " + else + jvmOpts+="-Xms${INIT_MEMORY:-$MEMORY} " + fi + + if isPercentage "${MAX_MEMORY:-$MEMORY}"; then + jvmOpts+="-XX:MaxRAMPercentage=$(getPercentageValue "${MAX_MEMORY:-$MEMORY}")" + else + jvmOpts+="-Xmx${MAX_MEMORY:-$MEMORY}" + fi logn '' curl -sSL -o ${tempDir}/BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar && \ diff --git a/scripts/start-finalExec b/scripts/start-finalExec index 10c2333f..08539bf7 100755 --- a/scripts/start-finalExec +++ b/scripts/start-finalExec @@ -368,10 +368,15 @@ fi if [[ ${INIT_MEMORY} || ${MAX_MEMORY} ]]; then log "Setting initial memory to ${INIT_MEMORY:=${MEMORY}} and max to ${MAX_MEMORY:=${MEMORY}}" - if [[ ${INIT_MEMORY} ]]; then + if isPercentage "$INIT_MEMORY"; then + JVM_OPTS="-XX:InitialRAMPercentage=$(getPercentageValue "${INIT_MEMORY}") ${JVM_OPTS}" + elif [[ ${INIT_MEMORY} ]]; then JVM_OPTS="-Xms${INIT_MEMORY} ${JVM_OPTS}" fi - if [[ ${MAX_MEMORY} ]]; then + + if isPercentage "$MAX_MEMORY"; then + JVM_OPTS="-XX:MaxRAMPercentage=$(getPercentageValue "${MAX_MEMORY}") ${JVM_OPTS}" + elif [[ ${MAX_MEMORY} ]]; then JVM_OPTS="-Xmx${MAX_MEMORY} ${JVM_OPTS}" fi fi @@ -431,15 +436,34 @@ elif [[ ${TYPE} == "CURSEFORGE" ]]; then copyFilesForCurseForge +if isPercentage "$INIT_MEMORY" || isPercentage "$MAX_MEMORY"; then +# Convert to bytes +NORM_INIT_MEM=$(normalizeMemSize "$INIT_MEMORY") +NORM_MAX_MEM=$(normalizeMemSize "$MAX_MEMORY") +# Convert to MB +((NORM_INIT_MEM*=1048576)) +((NORM_MAX_MEM*=1048576)) + + cat > "${FTB_DIR}/settings-local.sh" < "${FTB_DIR}/settings-local.sh" < 0.6) + scale=0.01 + ;; esac val=${1:0:-1} - echo $((val * scale)) + echo $((val * scale * mem)) } function compare_version() {