This book addresses a vital issue for all those developing software for the Java™ platform: how to achieve maximum performance and scalability for their applications.
Drawing on the authors' knowledge of the Java programming language and their extensive experience working on performance issues, the book reveals common mistakes and misconceptions concerning the performance characteristics of Java technologies. It offers overall development strategies and concrete, battle-tested techniques to dramatically improve the performance of applications constructed with the Java programming language.
Java™ Platform Performance highlights the importance of integrating performance evaluation into the application development process and discusses measurement techniques. The book then presents practical tactics for enhancing application performance in the areas of I/O, RAM footprint, small object management, algorithms, data structures, Swing, and deployment. Specific topics covered include:
With an understanding of the performance issues and specific techniques for reducing overhead discussed in this book, you will have the information you need to enhance the efficiency, speed, and scalability of your software.
I: STRATEGIES.1. What Is Performance?
Perceived Performance.2. The Performance Process.
Developing a Performance Process.
References on Object-Oriented Design.3. Measurement Is Everything.
Dealing with Flat Profiles.
II: TACTICS.4. I/O Performance.
Serialization.5. RAM Footprint.
Computing RAM Footprint.
What Contributes to Footprint?
Class Loading.6. Controlling Class Loading.
Eager Class Loading.
Reducing the Number of Classes.
Running Multiple Programs.7. Object Mutability: Strings and Other Things.
Lots of Little Objects.
Handling String Objects.
Mutable Objects in AWT and Swing.
Other Mutable Object Tactics.
Mutable Object Case Study.
Small Objects and the Modern JVM.
Array Mutability.8. Algorithms and Data Structures.
Using Recursive Algorithms.
Beyond Simple Algorithms.
Selecting Data Structures.
References on Algorithms and Data Structures.9. Using Native Code.
Native Graphics Example.
Examining JNI Costs.
Native Code Case Studies.10. Swing Models and Renderers.
Swing's Component Architecture.
Scalable Components.11. Writing Responsive User Interfaces with Swing.
Guidelines for Responsive GUIs.
Using Threads in Swing Programs.
Using Timers in Swing Applications.
Responsive Applications Use Threads.
Example: Searching the Web.12. Deployment.
Appendices.A. The Truth About Garbage Collection.
Why Should You Care About Garbage Collection?
The Guarantees of GC.
The Object Lifecycle.
References on Garbage Collection.B. The Java HotSpot Virtual Machine.
HotSpot Server Compiler.
In 1997, I was hired as a contractor to work on the Java(TM) Foundation Classes (JFC) Swing toolkit (Swing). This was an ambitious endeavor--Swing was slated to become the new standard for developing Graphical User Interfaces (GUIs) with the Java programming language. Prior to the release of Swing, the only GUI toolkit available with the Java platform was the Abstract Window Toolkit (AWT), a fairly primitive GUI toolkit by 1990s standards. While AWT was hobbled by a "lowest common denominator" design, Swing was designed to be a state-of-the-art toolkit. Written entirely in the Java programming language, it offered a powerful Model-View architecture, an advanced widget set, and a revolutionary pluggable look-and-feel (PLAF) system. When JFC was released in mid-1998, it was quickly adopted by thousands of eager developers.
As with any successful new product, along with stories of success came some bitter complaints. Some developers complained about architectural and philosophical issues. Others complained about bugs or the lack of a particular feature. However, the complaints I personally found most troubling were that programs written with Swing were slow.
I convinced my manager to let me spend a week looking into Swing's performance issues, downloaded a trial copy of a profiling package, and started poking at different parts of the toolkit.
It turned out that there were several areas where performance improvements could be made relatively easily. At the end of the week, I wrote a report on my findings and sent it to the rest of the Swing engineering team. Other members of the team got caught up in the spirit of performance tuning and began doing their own analyses. Over the next few months, I spent more and more of my time working on analysis and tuning and the Swing team made numerous performance enhancements. Many of the techniques described in this book are based on the knowledge gained while we were tuning Swing.
In late 1998, we shipped a new version of Swing that was more than twice as fast for typical tasks than the previous release. However, while many developers were pleased with the improvements, we were troubled to see that we still received numerous complaints about performance. Clearly the problem was more complex than we first thought.
I joined the performance team in Sun's Java Software unit in late 1998 and worrying about performance issues became my full-time job. In an effort to better understand the performance issues we and our developers face, I spend a lot of time talking with developers who are working on serious, real-world Java technology-based systems. Developers sometimes point out areas where changes in the libraries or VM could improve the performance of their programs. Part of my group's charter is to help make sure those changes, when appropriate, make their way into the runtime environment.
When working with developers, we also often find areas where changes to their program code can improve performance. We've found that there are a number of common mistakes and misconceptions about the performance characteristics of Java technologies and even about performance tuning in general.
The goal of this book is to share what we've learned about performance tuning Java technology-based systems with a wide audience. We hope that it will prove to be a valuable reference for you.
The information in this book will help you write high-performance software for the Java platform. It presents both high-level strategies for incorporating performance tuning into your software development process and code-level performance tuning tactics.
The two parts of the book approach performance tuning from different perspectives, providing a holistic view of the performance tuning process.
Unless otherwise noted, all performance measurements described in this book were run on a pre-release build of the Java 2 Standard Edition (J2SE) v. 1.3 using the HotSpot Client VM on the Microsoft Windows operating system.
Specific performance results are only representative of the configuration on which they are run. Factors such as the CPU, hard disk, operating system, and Java runtime environment (JRE) can all impact performance--keep in mind that the same benchmarks run under different configurations might yield substantially different results.
Complete code for the snippets, sample programs, utilities, and benchmarks used in this book is available online at http://java.sun.com/docs/books/performance/
We would like to thank the many people who contributed to the success of this book.
Jon Kannegaard, Larry Abrahams, and Graham Hamilton provided the initial push that started this project. We especially need to thank Larry, who provided much needed management support throughout the project. Lisa Friendly and Tim Lindholm, editors of the Java Series, patiently guided two first-time authors through the process of making this book a reality. Mike Hendrickson and Julie DiNicola from Addison-Wesley were immensely helpful throughout the entire process.
Deborah Adair of The Design Cage served as part editor, part graphic designer, and part writing coach. We couldn't have finished the project without her help.
Hans Muller, the technical lead for Project Swing and the foremost expert on Swing's threading model, provided the material for Chapter 11, Writing Responsive User Interfaces with Swing. He spent many nights and weekends working on this chapter so the rest of us could better understand how to use threads in Swing programs.
Alan Sommerer contributed to the outline and organization of early drafts, ensuring that key concepts were not missed.
David Wilson and Doris Chen began writing a two-day training course on performance tuning about the same time we started working on this book. We exchanged many ideas with them and believe both the course and the book benefited.
Over the past year, Agnes Jacob introduced us to many developers who had performance-related issues. These experiences were invaluable in deciding what information to include in this book.
Many people provided a tremendous amount of input to the book by reviewing early drafts or providing important technical tidbits: Eric Armstrong, Tom Ball, Clifford Crick, Mark Davidson, Joshua Engel, Peter Haggar, Howard Harkness, Cay Horstmann, Peter Kessler, Gary Little, Mike Martak, Mike McCloskey, Dave Mendenhall, Philip Milne, Srdjan Mitrovic, Bill Pataky, Nancy Schorr, and David Stoutamire.
Since the Java platform was introduced in 1995 it has become highly popular among both programmers and the companies they work for because it facilitates the rapid development and deployment of new software. From the start, however, Java technology has been dogged with complaints about its speed. A recent search of the USENET group comp.lang.java.programmer resulted in nearly 3,000 articles that contained the words "Java" and "slow." Clearly, performance is an area of concern for many developers.
In the past few years, there have been numerous technological advances that have improved the performance of the Java platform. Just-in-time (JIT) compilers and advanced runtime systems, such as Sun's Java HotSpot Virtual Machine, have significantly improved performance. In addition, as Moore's Law dictates, computers continue to get more powerful every year. Today's average PC is an order of magnitude faster than the average PC at the time Java technology was first introduced. Despite these advances, complaints about the speed of Java technology-based programs persist.
When average hardware performance continues to get faster, and the core runtime technology continues to improve, why do so many developers continue to have performance issues? The answer turns out to be somewhat obvious. The software being developed for the Java platform today is much more complex than it was just a year or two ago. Developers are continuing to push the envelope--each time the tools improve, the scope and complexity of the applications being built with them increases. In short, the technology is fulfilling the promise that it's not just for "Dancing Duke" applets anymore.
Powerful Java technology-based enterprise systems are being deployed in situations where millions of dollars are riding on the system's success. Independent software vendors (ISVs) are replacing and trying to compete with solutions written with more traditional languages like C++.
Not only do today's Java applications go far beyond making Duke dance, these large-scale, mission-critical software solutions are more complex than anything many Java programmers have written in the past. As they continue to push the limits of the technology, developers are also being asked to solve increasingly difficult problems that push the limits of their experience.
Developing high-performance software in any language is not a trivial task. In addition to a thorough understanding of the language and libraries you're using, you need to know how to effectively fit performance tuning into your overall development process.
This book presents strategies and tactics you can use to make performance tuning a part your software development process and effectively evaluate and optimize the performance of your software.