Java Solutions


Java Performance Tuning

reviewed by Marc Briand


Title:Java Performance Tuning
Author:Jack Shirazi
Publisher:September 2000, O'Reilly Books
Pages:426, softbound
ISBN:0-596-00015-4
Price:$34.95

So you bit the bullet and wrote that big application in Java, and now the boss says it runs too slow. You’ll probably want to get this book. It presents a wide variety of potential optimizations for Java programs, and chances are one or two will help you speed things up. A note to the truly desperate, however: this book is not (nor does it purport to be) a miracle cure. Of the many optimizations it presents, perhaps only a few will be appropriate for any single application. Also note that many of these optimizations may bring only modest performance gains, on the order of 10-30 per cent. That is just the nature of the optimization game. If you’re going to play, you can’t be an idealist.

The book begins with a discussion of tuning strategies and profiling. This seems appropriate, because it is where Java tuners should begin as well. Tuning a Java application without profiling it first is virtually guaranteed to be a waste of time. Here the author gives us a simple piece of advice: fix one thing at a time, and profile again. That’s because hot spots (the places that burn up the most time) tend to move around as you modify the code. If you do a lot of performance tuning, you probably knew that already, but it may not be obvious to newbies.

The bulk of the book is dedicated to the biggest performance problem areas in Java, with roughly one chapter given over to each culprit. There is a chapter each on object creation; exceptions, casts, and variables; loops and switches; I/O; sorting; threading; algorithms and data structures; distributed computing; and Strings — the only Java data type that gets a chapter all to itself.

Since Strings seem to be such a big problem area, you might naturally wonder if there is something wrong their design. I would say not necessarily. Remember that strings are among the most frequently used objects in any of the programming languages that support them. So even minor performance problems in a string class will tend to be magnified. And the Java String type is apt to be used naively, since it supplies a couple seemingly innocent functions which are in fact deadly in terms of performance. If Sun can be said to have erred, it was in making String so easy to misuse.

Using This Book

In many ways, tuning a Java app appears to be a thankless task. If you’re going to do it and enjoy it, you have to appreciate small victories, and you must have a flair for configuration management. That’s because many of the optimizations possible can be rendered either more effective, or ineffective, by choice of JVM, choice of compiler, and the multiple options under which they may be run. Note that this configuration dependence severely undercuts one of big Java’s selling points — its portability. So one way to use this book (admittedly, probably not a way the author intended) is to read it first to decide how badly you want to implement that next project in Java. However, this book does present some optimizations that appear to be fairly independent of configuration; it’s just too bad they are not organized as such. My first suggestion would be to go through this book with a highlighter and mark all the optimizations that are configuration independent. You could save yourself a lot of headaches down the road.

The author does provide checklists at the end of each chapter, of all the optimizations you might try, along with do’s and don’ts. A table might have been better, that organized optimizations by configuration sensitivity and projected performance gains (ballpark, of course).

No Religion Here

This is a fairly pragmatic book. At one point, when an optimization does not produce the results expected for a particular compiler, the author simply notes it and moves on, commenting that he doesn’t wish to get wrapped up in analyzing a particular JDK’s implementation. I think this is a sensible attitude; if you need better performance, you probably need it bad; there’s no time to obsess over something that doesn’t work.

In keeping with its pragmatic nature, the book contains numerous small tables that show the results of particular optimizations on specific compilers/platforms — typically JDK 1.1.6, JDK 1.2 with and without JIT, JDK 1.3, and HotSpot 1.0. These tables are useful and often contain surprising results, such as JDK 1.1.6 sometimes outperforming HotSpot 1.0. (Although the book’s discussions of HotSpot are few and far between, I found them very interesting; in general, the HotSpot results bring a sobering dose of reality to notions that some sort of wonder JVM, or even a hardware solution, might fix all of Java’s performance problems.)

I appreciate the author’s neutral stance with respect to Java. Java Performance Tuning takes no position on whether or not Java is a “good” language, or whether you should have used it in the first place. Instead, the book pretty much says, “I don’t care how you got here, but now that you are, here are some things that can help.” That’s a nice change of pace from all the hype.

Marc Briand is the former editor of C/C++ Users Journal. His interests include computer languages, graphics, animation, and just about any area where technical and aesthetic problems meet. He can be reached at marcbriand@cs.com.