2

I am using spring-boot-maven-plugin to build a docker image from my app with this command:

spring-boot::build-image

And I need to change default size of jvm direct memory (default is 10m) with this jvm argument:

-XX:MaxDirectMemorySize=64M

and this is my desperate attempt to do that in pom.xml of app where I was trying everything what came to my mind:

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <jvmArguments>-XX:MaxDirectMemorySize=64M</jvmArguments>
                            <environmentVariables>
                                <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS>
                                <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS>
                            </environmentVariables>
                            <systemPropertyVariables>
                                <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS>
                                <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                </executions>

                <configuration>
                    <layers>
                        <enabled>true</enabled>
                    </layers>
                    <image>
                        <name>docker.io/example/${project.artifactId}:${project.version}</name>
                    </image>

                    <excludeDevtools>true</excludeDevtools>

                    <systemPropertyVariables>
                        <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS>
                        <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS>
                    </systemPropertyVariables>
                    <environmentVariables>
                        <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS>
                        <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS>
                    </environmentVariables>
                    <jvmArguments>-XX:MaxDirectMemorySize=64M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005</jvmArguments>
                </configuration>
            </plugin>

But direct memory is still setup to 10M :( which I can see in log file when the app is booting up:

Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -agentpath:/layers/paketo-buildpacks_bellsoft-liberica/jvmkill/jvmkill-1.16.0-RELEASE.so=printHeapHistogram=1 -XX:ActiveProcessorCount=8 -XX:MaxDirectMemorySize=10M -Xmx11521920K -XX:MaxMetaspaceSize=144375K -XX:ReservedCodeCacheSize=240M -Xss1M -Dorg.springframework.cloud.bindings.boot.enable=true

Can anybody advise me what Am I doing wrong?

NOTES:

Spring Boot: 2.3.7.RELEASE

when I run docker image manually with -e, change of jvm argument is working:

docker run -e JAVA_TOOL_OPTIONS=-XX:MaxDirectMemorySize=64M  docker.io/example/app-name:0.0.1

this mvn plugin is using Buildpack to create the docker image:

https://paketo.io/docs/buildpacks/language-family-buildpacks/java/#about-the-jvm

UPDATE:

by the doc this should be working solution but it is not:

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                    </layers>
                    <image>
                        <name>docker.io/example/${project.artifactId}:${project.version}</name>
                        <env>
                            <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS>
                        </env>
                    </image>
                </configuration>
Dušan Salay
  • 309
  • 1
  • 12
  • Looks different, but is nearly the same as https://stackoverflow.com/a/65142031/4964553 . `JAVA_TOOL_OPTIONS` is a runtime environment variable - and thus needs to be passed to thecontainer with `docker run --env`, which was build by the `spring-boot-maven-plugin`. – jonashackt Jan 18 '21 at 15:22

3 Answers3

6

this is a working solution on how to make runtime env "sticky":

....
                    <image>
                        <name>docker.io/example/${project.artifactId}:${project.version}</name>
                        <env>
                            <BPE_APPEND_JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</BPE_APPEND_JAVA_TOOL_OPTIONS>
                            <BPE_DELIM_JAVA_TOOL_OPTIONS xml:space="preserve"> </BPE_DELIM_JAVA_TOOL_OPTIONS>
                        </env>
                    </image>
...

spring-boot-maven-plugin is using paketo buildpacks:

doc: https://github.com/paketo-buildpacks/environment-variables

Dušan Salay
  • 309
  • 1
  • 12
0

It depends on your goal here. It sounds like you want to change the max direct memory in your container when your app runs so you should be just doing what you indicated works here (and what's listed in the docs).

docker run -e JAVA_TOOL_OPTIONS=-XX:MaxDirectMemorySize=64M docker.io/example/app-name:0.0.1

The doc link you're referencing is talking about configuring options at runtime which is what you are doing with the env variables.

Daniel Mikusa
  • 13,716
  • 1
  • 22
  • 28
0

For anyone using Docker on Mac + Spring Boot's build-image, be aware that as of writing the default setting of Docker on Mac is a 2GB VM image that runs Linux with Docker running inside this linux VM.

With the current Spring 2.3.X default Pareto builder, this results in a docker image that has ~300MB available, but a fairly simple spring app (for me) needs ~600MB according to the memory calculator.

So no matter what the value of maxDirectMemorySize, it just would not work. As soon as I upped the mem available to the docker VM it worked just fine

Hamy
  • 20,662
  • 15
  • 74
  • 102