Optimizing Java

Books on Java performance are few and far between. From top publishers, we have Scott Oaks’ Java Performance (O’Reilly, 2014) as well as Java Performance Companion (Addison-Wesley, 2016), by multiple authors, which I reviewed in the September/October 2016 issue. The latter book was a descent into the workings of the G1 garbage collector and the Java Serviceability Agent. Optimizing Java is like the Oaks book in that it is an exhaustive explanation of the workings of the JVM, with a principal focus on the HotSpot implementation and OpenJDK-based variants.

The authors start with explanations of what performance is, how it’s measured, and the terminology used to refer to different aspects of performance. A quick overview of the JVM is then followed by a detailed discussion of hardware. This is necessary, because at numerous points in the rest of the book the authors refer to concepts that are explained in this hardware section: the CPU, I/O, memory paging, and other aspects of today’s systems. I admire the decision to anchor future discussions in the hardware, rather than generalizing in the abstract.

The book takes one additional step in preparing the reader for the forthcoming analysis: a detailed examination of how to run benchmarks (primarily using the Java Microbenchmark Harness) and how to understand the results obtained. The next three chapters are deep dives into the garbage collection (GC), including an excellent technical overview of GC in general, followed by analysis of how G1 and the parallel GC work in the present JVM. There follows a chapter on logging GC performance, and only then suggestions for how to tune the garbage collector for specific loads and execution situations. By the time you get to the tuning advice, you are fully capable of understanding the principles behind the suggestions.

The next four chapters explore how the JVM executes code. Authors Evans and Newland have been writing about code execution for this magazine for the last several issues. If you like their lucid explanations, you’ll love these chapters. They’re nitty-gritty nerd stim and highly readable. Then come two chapters on concurrency and parallel execution, one chapter on logging and efficient message processing, and one on profiling. A final section discusses the changes wrought by Java 9 and their effects on the previous material.

Optimizing Java is one of the most informative explanations of the internals of the JVM. Even if you’re not looking to solve a performance problem, you’ll benefit from reading it. And if you believe—as I do—that good programmers become better by knowing how exactly their code is executed, then you owe it to yourself to read this excellent volume.

Andrew Binstock

Andrew Binstock (@platypusguy) was formerly the editor in chief of Java Magazine. Previously, he was the editor of Dr. Dobb's Journal. He co-founded the company behind the open-source iText PDF library, which was acquired in 2015. His book on algorithm implementation in C went through 16 printings before joining the long tail. Previously, he was the editor in chief of UNIX Review and, earlier, the founding editor of the C Gazette. He lives in Silicon Valley with his wife. When not coding or editing, he studies piano.