#!/bin/bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Java launcher that configures memory based on how much is available.
# If the environment variable $JAVA_TOOL_OPTIONS is set (even if empty), we just
# launch the java command we are given. If it is not set, and we have a hint
# from the environment of what the memory size is, then we set JAVA_TOOL_OPTIONS
# to a value that should cause the JVM to use that memory size minus 80M.
# The JVM reads command-line options from this environment variable.
if [[ -z "${JAVA_TOOL_OPTIONS+x}" && -n "${X_GOOGLE_MEMORY_HINT_DO_NOT_USE}" ]]
then
  export JAVA_TOOL_OPTIONS="-XX:MaxRAM=$((${X_GOOGLE_MEMORY_HINT_DO_NOT_USE} - 80))m -XX:MaxRAMPercentage=100"
fi
exec "$@"

#!/bin/bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Java launcher that configures memory based on how much is available.
# If the environment variable $JAVA_TOOL_OPTIONS is set (even if empty), we just
# launch the java command we are given. If it is not set, and we have a hint
# from the environment of what the memory size is, then we set JAVA_TOOL_OPTIONS
# to a value that should cause the JVM to use 70% of that memory size.
# The JVM reads command-line options from this environment variable.
if [[ -z "${JAVA_TOOL_OPTIONS+x}" && -n "${X_GOOGLE_MEMORY_HINT_DO_NOT_USE}" ]]
then
  export JAVA_TOOL_OPTIONS="-XX:MaxRAM=${X_GOOGLE_MEMORY_HINT_DO_NOT_USE}m -XX:MaxRAMPercentage=70"
fi
exec "$@"

Change the heuristic for JVM memory options. Previously, for memory size `$N` (for example 256M), we did `-XX:MaxRAM=($N-80) -XX:MaxRAMPercentage=100`. It turns out that that doesn't work reliably for the 1G memory size, with containers being killed for using too much memory. So instead we do `-XX:MaxRAM=$N -XX:MaxRAMPercentage=70`, which seems to work reliably with sizes 256M, 512M, 1G, 2G. (It doesn't work reliably with 128M, but that's borderline anyway and our documentation doesn't talk about using it.)
Change the heuristic for JVM memory options. Previously, for memory size `$N` (for example 256M), we did `-XX:MaxRAM=($N-80) -XX:MaxRAMPercentage=100`. It turns out that that doesn't work reliably for the 1G memory size, with containers being killed for using too much memory. So instead we do `-XX:MaxRAM=$N -XX:MaxRAMPercentage=70`, which seems to work reliably with sizes 256M, 512M, 1G, 2G. (It doesn't work reliably with 128M, but that's borderline anyway and our documentation doesn't talk about using it.)

PiperOrigin-RevId: 327647485
Change-Id: I6fefc08fe8022f43d9a4043c7eeeb83d50c931b5
