Many people complain about the performance of their Java programs. Expecting miracles, or just giving in to hype, they want unoptimized programs to perform faster than natively C/C++-compiled code. I don't mean to say that everyone writes poorly written code, but unoptimized code is often less than perfect and can be tweaked to run faster while still being maintainable. In this article, I'll examine six Java books that explore ways to help you improve your Java programs' performance.
The six books are:
- Java Platform Performance: Strategies and Tactics, Steve Wilson and Jeff Kesselman (Addison-Wesley, January 2000)
- Java Performance Tuning, Jack Shirazi (O'Reilly & Associates, September 2000)
- Enterprise Java Performance, Steven Halter and Steven Munroe (Prentice Hall/Sun Microsystems Press, August 2000)
- Java Performance and Scalability, Volume 1: Server-Side Programming Techniques, Dov Bulka (Addison-Wesley, June 2000)
- Java 2 Performance and Idiom Guide, Craig Larman and Rhett Guthrie (Prentice Hall, August 1999)
- Practical Java Programming Language Guide, Peter Haggar (Addison-Wesley, February 2000)
Table 1 provides a quick look at the books' main characteristics. The first two rows should be self-explanatory. The remaining rows describe the books' coverage of improving performance in different Java technology areas.
|Table 1: Performance books overview|
|Java Platform Performance||Java Performance Tuning||Enterprise Java Performance||Java Performance and Scalability||Java 2 Performance and Idiom Guide||Practical Java|
|CD-ROM/Source from Web||No / Yes||No / Yes||No / Yes||No / Yes||No / No||No / Yes|
|Tuning Overview||Good||Very Good||Good||Poor||Average||None|
|Coding Techniques||Average||Good||Poor||Average||Good||Very Good|
|Data Structures||Good||Very Good||Poor||Average*||Average||Average|
|Distributed Computing||None||Good||Very Good||Very Good||Poor||None|
Scale: None, Poor, Average, Good, Very Good
A star (*) in a field means the review of the specific book provides further details of the ranking.
Now I'll review each book individually. The order in which I review them is random and not based on their rankings. Beside each book's title is a star rating that is based on the level of coverage and depth of techniques regarding Java performance tuning. One star is a poor rating; the highest rating (five stars) indicates exceptional coverage.
At only 230 pages -- the smallest of the bunch -- Java Platform Performance tries to pack lots of tips into a small amount of space. It does an excellent job, if your needs match the topics covered. Most readers should have an interest in a majority of the topics.
The book is divided nicely into two parts: one has information on the strategies involved in increasing performance, the other explains the tactics necessary to implement those strategies. Two in-depth appendices describe garbage collection and HotSpot. The garbage-collection chapter focuses mostly on the object life cycle, including a nice description of reference objects; the HotSpot chapter compares optimization techniques that HotSpot uses and the techniques you can use in your code.
The Strategies section, which is three chapters long, explains what performance is and how to measure it. I found the third chapter, which describes benchmarking and profiling-analysis techniques for performance measurement, most useful.
The meat of the book is the Tactics section, which covers nine topics, though not all are intended to help the reader improve performance:
- I/O Performance: Covers buffered I/O and serialization issues
- RAM Footprint: Describes how to measure memory usage
- Controlling Class Loading: Describes the nature of Java's dynamic class loading and techniques to improve it
- Object Mutability: Strings and Other Things: Covers the typical
StringBufferargument with techniques to reduce the number of created objects
- Algorithms and Data Structures: Selects the right collection class and algorithm
- Using Native Code: Describes the pitfalls of JNI usage with some short case studies
- Swing Models and Renderers: Discusses Swing's MVC architecture and how to best take advantage of it
- Writing Responsive User Interfaces with Swing: Describes
invokeAndWait()usage, as well as the
- Deployment: Covers deployment with JAR files
Overall, I found the techniques useful in the areas I was interested in: collections and Swing. The complete code examples are well done, as are the many tables comparing performance. You should incorporate several of the tactics into your everyday coding. And if you think JNI is the answer to your performance woes, this book helps explain why it isn't. The biggest problem that I found was the book's coverage of multithreading, which was limited to areas like Swing with
Java Performance Tuning -- the largest and newest book in the bunch -- is similar in style to Java Platform Performance, but boasts a much broader set of tactics and more in-depth explanations (in the form of real-world examples).
Java Performance Tuning offers optimization techniques on nearly everything a server-side developer would want to know -- with multiple reasons for not using many system classes. Jack Shirazi offers an enlightening tip on the performance (or lack thereof) of
StreamTokenizer. I think my days of using it are over.
Nine of the book's fifteen chapters -- described in the following list -- cover the bulk of the optimization techniques. The remaining chapters help get you started with tools and describe tuning techniques beyond the code level, like things you can do with the underlying system. The final chapter is more of an appendix with a list of resources.
- Object Creation: Shows how to reduce the number of objects created
- Strings: Describes improvements in comparing and searching
- Exceptions, Casts, and Variables: Covers access usage optimizations
- Loops and Switches: Describes some general optimization coding techniques
- I/O, Logging, and Console Output: Discusses replacing
System.out, serialization optimizations, and compression (though it offers no code for compression)
- Sorting: Uses hand-crafted sorting
- Threading: Discusses how to better design and limit the number of locked resources
- Appropriate Data Structures and Algorithms: Covers selecting the right collection class and algorithm; includes a nice hash map to match partial string keys
- Distributed Computing: Describes ways to optimize message passing and data transfers
Each chapter ends with a "performance checklist" to help remind you to perform the code-tuning tasks discussed. The checklists can also help you refresh your knowledge weeks or months after you've read the book.
Experienced Java developers will like the techniques described in Java Performance Tuning and should be able to improve performance considerably by implementing them.
Coming in from left field is Enterprise Java Performance. This book is so unlike the others that I'm not sure where to start describing it. Probably the simplest way to start is with the authors. Enterprise Java Performance is by Steven Halter and Steven Munroe -- two of the leaders of IBM's SanFrancisco project. SanFrancisco is a business component framework for Java that helps developers of large distributed business/e-commerce applications start from a prebuilt set of business objects. The emphasis is on large here. Halter and Munroe's experience with large systems plays out well in the book.
With that said, what is the book about?
The first two parts of Enterprise Java Performance help you to understand the needs of large-scale performance optimization: from the tools and coding techniques in Part 1 to the discussion of various performance issues in Part 2 (dubbed "Performance Physics"). There are about 160 pages on the optimization techniques the other books spend all their pages on, such as local vs. remote issues and locking bottlenecks. While I found nothing wrong with the content, Enterprise Java Performance didn't match the other books' depth in those areas; I was hoping to find more code improvements that tell you to do this or that. However, Enterprise Java Performance does have information about enterprise-level applications that the other books do not.
In Part 3, you'll meet BOB (business object benchmark), as well as several other benchmarks. BOB goes beyond the common Java benchmarks available today; it includes application-level benchmarking and measures performance at a higher level of application functionality. If you like BOB, you can modify it to measure your own applications' performance.
Part 4 introduces the book's core application environments: SanFrancisco, Enterprise JavaBeans, CORBA, and Jini. This sounds great, but Halter and Munroe spend most of the time describing each particular environment -- leaving only about a page for performance issues.
Parts 5 and 6 stand out as some of the most useful material in the book, bringing in bits from the first two parts. They basically help you review design decisions you must make when developing large-scale systems. Remember, though, the discussion is about large-scale design needs, so you won't get the answer to "Do I try to reduce the number of classes in order to load faster, and should I compress my JAR file or not?"
For the right audience, Enterprise Java Performance could be very helpful. That audience -- developers of huge applications -- may explain why the book costs 25 percent more than the others.
On a side note, I found it interesting that the technical editors, Peter van der Linden, Steve Pietrowicz, and Achut Reddy, are almost as prominently placed on the book's cover as the two authors. I have never heard of Reddy, but the other two should be recognizable to many developers; van der Linden is the popular author of Just Java and other books, and Pietrowicz is a former JavaWorld columnist.
Java Performance and Scalability
is clearly marketed to the server-side developer. Beginning- to intermediate-level programmers may find the many performance-boosting techniques most useful.
The book includes 48 optimization techniques described in the first eight chapters, and server-side-specific content -- RMI, Servlet, and JDBC performance-enhancing techniques - described in the last three chapters and two code-based appendices.
Here are the eight chapters on optimization:
- Java Strings: Eight optimization tips on using Strings
- Pure Overhead: Eight coding tips
- Vectors and Hashtables: Eight ways to avoid using the collection framework
- Caching: Five ways to avoid costly computations
- I/O Streams: Six ways to improve I/O usage, mostly by buffering
- Recycling: Three ways of recycling and pooling
- Scalability and Multithreading: Seven ways to work better with threads
- JNI: Three tips for using or not using JNI
The optimization methods usually include helpful descriptions, tables showing performance improvements, and lots of code. The book also shows six cycles of optimizing a Java-based Web server.
Surprisingly, the book offers lengthy coverage of the
Vector classes. The content is good, but the discussion completely ignores one of the book's own tips: avoid unnecessary synchronization. While those two classes are frequently used, I was expecting at least a minimal discussion on the use of
HashMap isn't even mentioned, and the
ArrayList discussion is reserved for the multithreading section, not the one on datastructures.
Due to the book's shortcomings, I am eagerly awaiting Volume 2.
The only hardcover book in the bunch, Java 2 Performance and Idiom Guide, presents itself as a series of techniques for improving your Java code's performance and increasing the speed at which you produce that code. There are even some short case studies, which serve to prove various points. As the cover states, the book hopes to help you "write fast code" and "write code fast."
Divided into about six sections, this guide offers simple tips for improving the performance of your Java programs, and addresses concerns about some library and language usage. You'll also find a chapter on testing that describes test suites and JUnit. The performance-specific content is a little more than one-third of the book; the rest deals with topics like concurrency, collections, and reflection. The book ends with a series of style guidelines.
The optimization techniques provide no earth-shattering revelations. That doesn't mean the techniques are bad (well, excluding those I've noted below) -- the presentation just leaves a bit to be desired. The technique descriptions lack depth, and the limited set of standard libraries described is not covered fully. Craig Larman and Rhett Guthrie barely break away from the
java.util packages, offering nothing for the GUI developer and very little for the enterprise developer.