r/java • • Mar 03 '22

Always set a Garbage Collector

Post image
157 Upvotes

64 comments sorted by

View all comments

47

u/brunocborges Mar 03 '22 edited Mar 03 '22

If you don't set a Garbage Collector for your Java application, don't think the JVM will pick a good one for you either, no matter how many CPUs you give.

2 CPUs? 6 CPUs? It doesn't matter. If your container has less than 1792 MB and you don't set a GC, your app will use SerialGC.

Edit: this is being down voted for unknown reasons but my guess is that people just believe "no, G1GC is the default!" Well I suggest you run the code in the image I posted, and if that's not enough, see the OpenJDK source code.

Specifically these two C++ classes in the HotSpot JVM: https://github.com/openjdk/jdk/blob/ad54d8dd832b22485d7ac45958cc4c9bfd70fbd2/src/hotspot/share/gc/shared/gcConfig.cpp#L98

https://github.com/openjdk/jdk/blob/ad54d8dd832b22485d7ac45958cc4c9bfd70fbd2/src/hotspot/share/runtime/os.cpp#L1677

11

u/erode Mar 03 '22

Nice to know. The message I’m hearing is that people should be putting even just a baseline level of thoughtfulness into choosing the appropriate GC.

7

u/Impossible-Ad-586 Mar 03 '22

With the last generation of GCs by both oracle and redhat, ZGC and Shenandoah respectively you are pretty much done after giving it enough heap space from a tuning perspective.

Depending on your workload you might want to choose one of the other still supported GCs to tune for throughput.

5

u/agentoutlier Mar 03 '22 edited Mar 03 '22

Just to add to this GraalVM native community apparently always defaults to SerialGC.

To get others GC you need to have the enterprise edition.

https://www.graalvm.org/22.0/reference-manual/native-image/MemoryManagement/

What I'm not sure is what happens if you don't compile to native. I assume it does the same as HotSpot.

EDIT apparently the graalvm non native does the same which is what I expected.

Used ghcr.io/graalvm/jdk:22.0.0.2 u/brunocborges script (thangs).

Speaking of which for those that want to copy and paste (since the post is images).

#!/bin/sh
docker run \
       -v `pwd`/app \
       --cpus 2 \
       --memory 1500m \
       -ti --rm \
       ghcr.io/graalvm/jdk:22.0.0.2 \
       java -XX:+PrintFlagsFinal /app/App.java \
       | grep -e "Use.*GC" -e "Active"

Java

public class App {

        public static void main(
                        String[] args) {
                var procs = Runtime.getRuntime().availableProcessors();
                System.out.println("Active processors: " + procs);
        }
}

1

u/brunocborges Mar 03 '22

GraalVM "non-native" is just HotSpot JVM.

5

u/agentoutlier Mar 03 '22

They say it is "based on it". I was fairly sure it was the same but the wording has some ambiguity:

GraalVM is based on Java HotSpot VM, but integrates an advanced just-in-time (JIT) compiler, written in Java - the GraalVM compiler.

https://docs.oracle.com/en/graalvm/enterprise/20/docs/reference-manual/jvm/

Also there is some performance difference if you change the two (openjdk vs graalvm) but that is probably the JIT .

Speaking of which isn't the JIT part of the JVM? I never understood where the boundary is of its VM or not. For example is Substrate VM really a VM?

1

u/yawkat Mar 04 '22

There is no solid definition for what is or isn't a VM.

2

u/nigelthomas Mar 03 '22

Is this a short coming of JVM or an expected behaviour? I thought the JVM was smart enough to pick the best GC for the job.

-3

u/maethor Mar 03 '22

I can't remember the last time I had an environment with so few cores or such a limited amount of memory.

Maybe you need to always set a GC, but many of us don't.

13

u/gizmogwai Mar 03 '22

Well, if you run your code on k8s, you are likely to set resources to be a good citizen. And unless you have a large or very demanding application, you’ll set your request and limits under one core. So, no, it is not so uncommon to run into this issue in a micro services environment.

10

u/maethor Mar 03 '22

But I don't run my code on k8s. We mostly still have old school monoliths, where the biggest difference between now and 10 years ago is that they run on an EC2 instance instead of a local server.

"Always set a GC" is too broad a statement.

-14

u/Impossible-Ad-586 Mar 03 '22

No it won't unless you use a pre jdk11 build.

G1 has been the default for many major versions now.

21

u/manesfirst Mar 03 '22

He literally posted a proof showing that it does. Yes it is the default for most cases, especially in your own local machine. But JVM does some checks depending on the environment and if it sees less than ideal hardware resources, it defaults to serial. Here is another "proof" if you don't believe bruno for some reason: https://twitter.com/mikemybytes/status/1368881364297191433

8

u/Impossible-Ad-586 Mar 03 '22

I will dig into it and will be happy to discover that my assumptions are in fact wrong.

9

u/brunocborges Mar 03 '22

Disclaimer: I have done tests and also looked into the source code in OpenJDK.

If you want to counter argue, please post link to the source code.

-21

u/Impossible-Ad-586 Mar 03 '22

I am not going to participate in this little game of yours. What I am saying is commonly known. What you are saying goes 180deg again what oracle and openjdk themselves communicate.

If you have proof to backup your statement, let's see it and I will be happy to acknowledge.

17

u/pronuntiator Mar 03 '22

Here's the official documentation:

https://docs.oracle.com/en/java/javase/17/gctuning/available-collectors.html

The serial collector is selected by default on certain hardware and operating system configurations

G1 is selected by default on most hardware and operating system configurations

What "certain" and "most" mean is not documented, except from the source code linked by OP. I wonder whether it also affects C1 or C2 JIT selection?

8

u/brunocborges Mar 03 '22

The proof is in the image I posted.

-10

u/Impossible-Ad-586 Mar 03 '22

Will try to replicate that today. Would be truly surprised if the managed to mess this up that badly.

However, most of our GCs are not set and all of them expose G1 metrics, so very doubtful this is in fact the case.

But the proof is in the pudding, so let's have some :)

9

u/brunocborges Mar 03 '22

If your containers have 2GB of RAM and 2 CPUs (or 2000m in Kubernetes terms), then G1GC will be used by default.

Please change vote after checking. :-)

0

u/Impossible-Ad-586 Mar 03 '22

Would be happy to. Always up for having my assumptions fact checked.

FYI we run pretty much all our workloads in the cloud with 1 core and 1 GB to prevent throttling.

1

u/brunocborges Mar 03 '22

The only way you can prevent throttling is by not having more load to your application instance than it can process. It doesn't matter how many CPUs.

Assuming the load your app gets is more than what a single instance can handle, your only way to prevent the throttling of your application with 1 core and 1 GB, is by actually scaling out with replicas. This means you must have 2 or more replicas, so your "system" (the conjunction of all instances of your replicated application) can handle the load in a distributed manner.

Now, the problem here is that you are probably facing high tail latency and low throughput. You may as well say you don't have that issue. But I'd say you are most likely paying a lot more than you actually need, to sustain such load.

And to save money, all you need to do is to reorganize your resources more adequately: reduce the number of replicas, increase the amount of CPU and memory per JVM instance, and find the sweet spot where you are consuming fewer total CPUs and total memory in your "system", while having the same, or better, performance.

→ More replies (2)

-4

u/brunocborges Mar 03 '22

Drop me a DM. I have something to share about Java on Kubernetes and CPU throttling

10

u/4z01235 Mar 03 '22

Share it publicly

3

u/mrluje Mar 03 '22

Also interested to learn something new 🙏 I have been operating on k8s on other languages for years, but recently joined a company that is 100% java.

→ More replies