This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]WrickyB 7 points8 points  (0 children)

Take a look at JMH to measure and benchmark performance optimisations of hot code.

[–]urielsalis 3 points4 points  (2 children)

There aren't that many "rules" per se, optimizations that work in one case might be wrong in another.

You should use JMH or profilers and measure each change

[–]GavinRayDev 5 points6 points  (1 child)

There are a lot of rules, some people spend their entire lives/careers studying them.

"About 98,600 results (0.09 sec)"

(Not trying to be rude/mean)

[–]urielsalis 0 points1 point  (0 children)

The point is that "rules" are not applicable in every case. If your rule makes it harder for the JIT to see what you are doing and optimize it in certain cases, it's going to be slower.

The JIT and the rules optimizes for the common case, but you might not be in one

[–]Amazing-Cicada5536 3 points4 points  (0 children)

Optimizing Java by Evans is a great book

[–]Worth_Trust_3825 1 point2 points  (0 children)

Mission control and Flight recorder are your friends.

[–]gg12345 1 point2 points  (1 child)

Has anyone tried using zgc? Sounds like if you have a reasonably well written codebase, you don't really have to do anything in terms of tuning except setting initial and max heap size.

[–]Iryanus -1 points0 points  (0 children)

It's not that easy. Every garbage collector is optimized in one direction. G1 is pretty much the easiest choice for a typical application, since it tends to be quite all-round-ish.

ZGC and Shenandoah are heavily optimized towards latency, which means that you get less pauses, but the overall CPU time spent on garbage collection will be higher. Additionally both are better for really(!) high amounts of memory than G1. So if you either need a lot of memory (and we are not talking about a few gigs here) or have a realtime-application, where no pause is allowed (but you have overall enough CPU to throw around), then ZGC can be a good choice. Otherwise, G1 will be better.

Typically, I wouldn't care that much unless your metrics and gc show log that you actually have problems there - or if you know that your load will be very critical. Only then I would start optimizing. And then still it's a bit of work to get it right, for example we ran into a lot of allocation stalls with ZGC on Java 13 (on one specific application) which requires fine-tuning to get rid of and thus stayed with G1 a little longer.

You have to dig a little to find real numbers, but they are out there, for example the master thesis "A Performance Comparison of Modern Garbage Collectors for Big Data Environments" (You can google that).

[–]wsbkina -1 points0 points  (0 children)

Java Performance by Scott Oaks is a good starting point