Remix.run Logo
▲ gavinray a day ago

Modern JVM's (JDK +10) have runtime detection for whether they're operating in a container environment, and apply sensible defaults based dynamically on resources if so.

Google term: "-XX:+UseContainerSupport"

▲hibikir a day ago | parent | next [-]

While you are replying to someone that definitely knows this, you'd be surprised by the number of production containers out there, in big companies, that haven't turned on container support, or hand tuned their memory well, and thus end up throwing away large parts of the memory they are allocated.

Having used Java since the days where you needed to do very heavy tuning to get it to run properly, it all seems alien, as understanding the old garbage collectors was basically mandatory 20 year ago. But even if you reply to someone that does understand the new features, you might be helping a bunch of people that don't. In my unit of about 100 programmers I am the only one that even bothered looking, given how every single pod we deployed was either completely unconfigured, or so badly misconfigured, using settings that haven't been ideal since Java 6.

In one of the pods, running a batch job that was using way too much memory, about a third of the memory in the old generation were millions of copies of the very same string. Interning of strings loaded like that has been a JVM option since 2014.

▲gavinray a day ago | parent [-]

On that note, here's my favorite little-known tip for running Docker/Container JVM apps:

The Redhat image distros, like UBI and the JDK ones, have a really handy bash script, "run-java.sh". It's a wrapper script for best-practice and intelligent JVM runtime args inside of container envs:

  /opt/jboss/container/java/run/run-java.sh
It started life as fabric8's run-java-sh, and it's what the Dockerfile.jvm generated by Quarkus apps uses as its entrypoint.

The script handles, among other things:

- Heap sized as a percentage of the container's memory limit via -XX:MaxRAMPercentage (default 80%, compared with JVM's default of 25%). Tunalbe with JAVA_MAX_MEM_RATIO env.

- -XX:+ExitOnOutOfMemoryError, so an OOM kills the container and your orchestrator restarts it

- Defaults for GC and metaspace

- JAVA_DEBUG=true / JAVA_DEBUG_PORT=*:5005 turns on JDWP without you having to edit the Dockerfile

- http_proxy/https_proxy/no_proxy env vars get translated into -Dhttp.proxyHost etc

▲brunoborges 19 hours ago | parent [-]

Yeap! JVM launchers are extremely helpful. At Microsoft, I helped design `jaz` (https://aka.ms/jaz) for this purpose!

▲brunoborges 19 hours ago | parent | prev | next [-]

When cgroups / container awareness was added to OpenJDK, it was not on by default, and indeed there were versions that needed this flag. However, all modern JDKs 11 and later, have container awareness support on by default.

That said, this only makes the HotSpot JVM use cgroups data to interpret CPU and Memory limits. It does not make any special consideration of default ergonomics (thread pools, GC selection, heap sizing, and so on). Before the container awareness support, the JDK would read the VM/bare metal data; in other words, pre-container awareness support would result in this: if the VM has 32GB of RAM and a container is set for 4GB of memory limit, the JVM reads the 32GB as available memory.

All the defaults I mentioned still apply even inside a container.

Edit: no worries! thanks for sharing!

▲gavinray a day ago | parent | prev | next [-]

I just realized the username of the comment I replied to.

The OP undoubtedly knows this, whoops.

▲froh a day ago | parent [-]

I (and presumably some others) didn't. TIL and thx for sharing.

and I also read it not as contradiction but simply as useful additional information.

▲vips7L 21 hours ago | parent | prev [-]

Isn’t that on by default?

▲brunoborges 17 hours ago | parent [-]

Pretty much, yes.

▲vips7L 13 hours ago | parent [-]

So is there nuance in pretty much? My understanding for a while is that hotspot automatically detected containers.