Tune up your Java programs with the help of these Java books 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 PerformanceJava Performance TuningEnterprise Java PerformanceJava Performance and ScalabilityJava 2 Performance and Idiom GuidePractical JavaPrice4.954.959.994.959.992.95Total Pages230430410290300280CD-ROM/Source from WebNo / YesNo / YesNo / YesNo / YesNo / NoNo / YesTuning OverviewGoodVery GoodGoodPoorAverageNoneCoding TechniquesAverageGoodPoorAverageGoodVery GoodObject LoadingGoodAverageNoneAverageAverageAverageData StructuresGoodVery GoodPoorAverage*AverageAverageThreadsPoor*Very GoodPoorGoodAverage*GoodGUIsGoodPoorNoneNoneNoneNoneI/OAverageGoodPoorAverageAverageAverageDistributed ComputingNoneGoodVery GoodVery GoodPoorNoneJNIGoodNoneNoneAverageNoneNoneScale: None, Poor, Average, Good, Very GoodA 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 issuesRAM Footprint: Describes how to measure memory usageControlling Class Loading: Describes the nature of Java’s dynamic class loading and techniques to improve itObject Mutability: Strings and Other Things: Covers the typical String vs. StringBuffer argument with techniques to reduce the number of created objectsAlgorithms and Data Structures: Selects the right collection class and algorithmUsing Native Code: Describes the pitfalls of JNI usage with some short case studiesSwing Models and Renderers: Discusses Swing’s MVC architecture and how to best take advantage of itWriting Responsive User Interfaces with Swing: Describes invokeLater() and invokeAndWait() usage, as well as the Timer classDeployment: Covers deployment with JAR filesOverall, 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 invokeLater() and invokeAndWait(). 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 createdStrings: Describes improvements in comparing and searchingExceptions, Casts, and Variables: Covers access usage optimizationsLoops and Switches: Describes some general optimization coding techniquesI/O, Logging, and Console Output: Discusses replacing System.out, serialization optimizations, and compression (though it offers no code for compression)Sorting: Uses hand-crafted sortingThreading: Discusses how to better design and limit the number of locked resourcesAppropriate Data Structures and Algorithms: Covers selecting the right collection class and algorithm; includes a nice hash map to match partial string keysDistributed Computing: Describes ways to optimize message passing and data transfersEach 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 StringsPure Overhead: Eight coding tipsVectors and Hashtables: Eight ways to avoid using the collection frameworkCaching: Five ways to avoid costly computationsI/O Streams: Six ways to improve I/O usage, mostly by bufferingRecycling: Three ways of recycling and poolingScalability and Multithreading: Seven ways to work better with threadsJNI: Three tips for using or not using JNIThe 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 Hashtable and 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 and ArrayList with Hashtable and Vector. 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.lang and java.util packages, offering nothing for the GUI developer and very little for the enterprise developer.Java 2 Performance and Idiom was released in the summer of 1999, making it the oldest book I reviewed here. Since then, at least one of the techniques mentioned has been proven not to work: using double-checked locking to reduce synchronization. (See the Resources section for “The ‘Double-Checked Locking is Broken’ Declaration.”) In addition, updates to core classes made after the book’s publication are obviously not reflected; for instance, as of Java 1.3, string hash codes are now cached. The time tests have also become dated; JDK 1.2.1 was the latest version at the time of the book’s publication (so was Microsoft SDK 3.1).Peter Haggar’s book is similar in style and structure to Scott Meyers’s Effective C++ and More Effective C++. Instead of chapters, Practical Java is broken down into 68 praxes, a term Haggar uses to describe his tips. According to my Random House dictionary, the term means “a set of examples for practice” or “practice, as distinguished from theory,” both of which seem to fit well. The 68 tips, 17 of which are specific to performance tuning, are broken into 6 sections: General Techniques: 7 rules every programmer should be aware ofObject Equality: 8 rules of equals() usageException Handling: 12 tips on exception usagePerformance: 18 ways to optimize your codeMultithreading: 13 tips on threadingClasses and Interfaces: 10 rules of good class designAlthough not strictly a performance-tuning book, I thought the optimization and coding techniques were thought out and explained well. Everything was easy to follow, and nothing was too advanced. You can think of the 68 praxes as do’s and don’ts for Java development. If you follow the rules, your programs will be in great shape; they should perform better too. I like Practical Java Programming Language Guide, but its lack of range keeps me from giving it five stars. Most of the praxes are too simple. There’s even overlapping in some topics; for example, Praxis 15 is “Follow these rules when implementing an equals() method,” while Praxis 11 is “Implement the equals() method judiciously.” Praxis 15 could just be added to the end of Praxis 11, with no loss in presentation or meaning.For developers about to take the Part 2 exam for Java developer certification, this book could be a great help. The well-thought-out reasoning for each praxis could help you justify what you designed in Part 1.How big of a bag to bring to the bookstore?Which book or books you buy depends on where you are in your Java learning cycle. If you are just getting started with Java development, Practical Java and Java Platform Performance would be worthy additions to your library. Both will help you write cleaner, more efficient Java code. If you can only get one of the two, get the former — though if you need some help with Swing development, Java Platform Performance may be better. (Of course, being biased, I would recommend my Swing book if you really need help with Swing.) To get the biggest bang for your buck and maximize your program’s efficiency, the experienced developer will find Java Performance Tuning the best resource. No other book approaches the quality and depth of its techniques. Giving copies of this book to every member of your development team will save you money in the long run.I liked some of the techniques in Java 2 Performance and Idiom Guide, but its age seems to be its downfall in this ever-changing Java world. You will pick up some nice tips if you are new to Java, but its small size and dated material make the newer offerings better choices.Also, don’t forget about High-Performance Java Platform Computing from Thomas Christopher and George Thiruvathukal, reviewed in “Java Threads: A Comparative Book Review” (JavaWorld, December 15, 2000). (See Resources for a link.) It, too, may be of interest to those who want to boost their applications’ performance. John Zukowski is the founder of the strategic Java consulting business JZ Ventures, a lecturer on Web technologies for Northeastern University in Boston, and the author of the Definitive Guide to Swing for Java 2 (Second Edition) from Apress. Build Automation