Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (523.48 KB, 7 trang )
<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1></div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>
<b>Java Performance Tuning </b>
Copyright © 2000 O'Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O'Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472.
The O'Reilly logo is a registered trademark of O'Reilly & Associates, Inc. Many of the designations
used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where
those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks,
and The Java™ Series is a trademark of O'Reilly & Associates, Inc. The association of the image of
a serval with the topic of Java™ performance tuning is a trademark of O'Reilly & Associates, Inc.
Java™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun
Microsystems, Inc., in the United States and other countries. O'Reilly & Associates, Inc. is
independent of Sun Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates,
Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher assumes no
responsibility for errors or omissions, or for damages resulting from the use of the information
contained herein.
While every precaution has been taken in the preparation of this book, the publisher assumes no
responsibility for errors or omissions, or for damages resulting from the use of the information
contained herein.
<b>Java Performance Tuning </b>
Preface - 5
Contents of This Book
Virtual Machine (VM) Versions
Conventions Used in This Book
Comments and Questions
Acknowledgments
1. Introduction - 7
1.1 Why Is It Slow?
1.2 The Tuning Game
1.3 System Limitations and What to Tune
1.4 A Tuning Strategy
1.5 Perceived Performance
1.6 Starting to Tune
1.7 What to Measure
1.8 Don't Tune What You Don't Need to Tune
1.9 Performance Checklist
2. Profiling Tools - 21
2.1 Measurements and Timings
2.2 Garbage Collection
2.3 Method Calls
2.5 Monitoring Gross Memory Usage
2.6 Client/Server Communications
2.7 Performance Checklist
3. Underlying JDK Improvements - 55
3.1 Garbage Collection
3.2 Replacing JDK Classes
3.3 Faster VMs
3.4 Better Optimizing Compilers
3.5 Sun's Compiler and Runtime Optimizations
3.6 Compile to Native Machine Code
3.7 Native Method Calls
3.8 Uncompressed ZIP/JAR Files
3.9 Performance Checklist
4. Object Creation - 77
4.1 Object-Creation Statistics
4.2 Object Reuse
4.3 Avoiding Garbage Collection
4.4 Initialization
4.5 Early and Late Initialization
4.6 Performance Checklist
5. Strings - 97
5.1 The Performance Effects of Strings
5.2 Compile-Time Versus Runtime Resolution of Strings
5.3 Conversions to Strings
5.4 Strings Versus char Arrays
5.5 String Comparisons and Searches
5.6 Sorting Internationalized Strings
5.7 Performance Checklist
6. Exceptions, Casts, and Variables - 135
6.1 Exceptions
6.2 Casts
6.3 Variables
6.4 Method Parameters
6.5 Performance Checklist
7. Loops and Switches - 144
7.1 Java.io.Reader Converter
7.2 Exception-Terminated Loops
7.3 Switches
7.4 Recursion
7.5 Recursion and Stacks
7.6 Performance Checklist
8. I/O, Logging, and Console Output - 167
8.1 Replacing System.out
8.2 Logging
8.3 From Raw I/O to Smokin' I/O
8.4 Serialization
8.5 Clustering Objects and Counting I/O Operations
8.6 Compression
8.7 Performance Checklist
9. Sorting - 191
9.1 Avoiding Unnecessary Sorting Overhead
9.2 An Efficient Sorting Framework
9.3 Better Than O(nlogn) Sorting
9.4 Performance Checklist
10.1 User-Interface Thread and Other Threads
10.2 Race Conditions
10.3 Deadlocks
10.4 Synchronization Overheads
10.5 Timing Multithreaded Tests
10.6 Atomic Access and Assignment
10.7 Thread Pools
10.8 Load Balancing
10.9 Threaded Problem-Solving Strategies
10.10 Performance Checklist
11. Appropriate Data Structures and Algorithms - 233
11.1 Collections
11.2 Java 2 Collections
11.3 Hashtables and HashMaps
11.4 Cached Access
11.5 Caching Example I
11.6 Caching Example II
11.7 Finding the Index for Partially Matched Strings
11.8 Search Trees
11.9 Performance Checklist
12. Distributed Computing - 264
12.1 Tools
12.2 Message Reduction
12.3 Comparing Communication Layers
12.4 Caching
12.5 Batching I
12.6 Application Partitioning
12.7 Batching II
12.8 Low-Level Communication Optimizations
12.9 Distributed Garbage Collection
12.10 Databases
12.11 Performance Checklist
13. When to Optimize - 281
13.1 When Not to Optimize
13.2 Tuning Class Libraries and Beans
13.3 Analysis
13.4 Design and Architecture
13.5 Tuning After Deployment
13.6 More Factors That Affect Performance
13.7 Performance Checklist
14. Underlying Operating System and Network Improvements - 304
14.1 Hard Disks
14.2 CPU
14.3 RAM
14.5 Performance Checklist
15. Further Resources - 315
15.1 Books
15.2 Magazines
15.3 URLs
15.4 Profilers
15.5 Optimizers
Performance has been an important issue with Java™ since the first version hit the Web years ago.
Making those first interpreted programs run fast enough was a huge challenge for many developers.
Since then, Java performance has improved enormously, and any Java program can now be made to
run fast enough provided you avoid the main performance pitfalls.
This book provides all the details a developer needs to performance-tune any type of Java program.
I give step-by-step instructions on all aspects of the performance-tuning process, right from early
considerations such as setting goals, measuring performance, and choosing a compiler, to detailed
examples on using profiling tools and applying the results to tune the code. This is not an
entry-level book about Java, but you do not need any previous tuning knowledge to benefit from reading
it.
Many of the tuning techniques presented in this book lead to an increased maintenance cost, so they
should not be applied arbitrarily. Change your code only when a bottleneck has been identified, and
never change the design of your application for minor performance gains.
<b>Contents of This Book </b>
Chapter 1 gives general guidelines on how to tune. If you do not yet have a tuning strategy, this
chapter provides a methodical tuning process.
Chapter 2 covers the tools you need to use while tuning. Chapter 3 looks at the Java Development
Kit™ ( JDK, now Java SDK), including VMs and compilers.
Chapter 4 through Chapter 12 cover various techniques you can apply to Java code. Chapter 12
looks at tuning techniques specific to distributed applications.
Chapter 13 steps back from the low-level code-tuning techniques examined throughout most of the
book and considers tuning at all other stages of the development process.
Chapter 14 is a quick look at some operating system-level tuning techniques.
Each chapter has a performance tuning checklist at its end. Use these lists to ensure that you have
not missed any core tuning techniques while you are tuning.
<b>Virtual Machine (VM) Versions </b>
I have focused on the Sun VMs since there is enough variation within these to show interesting
results. I have shown the time variation across different VMs for many of the tests. However, your
main focus should be on the effects that tuning has on any one VM, as this identifies the usefulness
of a tuning technique. Differences between VMs are interesting, but are only indicative and need to
be verified for your specific application. Where I have shown the results of timed tests, the VM
versions I have used are:
<i>1.1.6</i>
Version 1.1.x VMs do less VM-level work than later Java 2 VMs, so I have used a 1.1.x VM
that includes a JIT. Version 1.1.6 was the earliest 1.1.x JDK that included enough
Intuitive System's OptimizeIt! ()
KL Group's JProbe ()
CodeWizard for Java from ParaSoft Corporation (
PureLoad from PureIT AB (
SilkObserver from Segue Software, Inc. ()
SockPerf from IBM alphaWorks (
TrueTime/DevPartner Java Edition from Compuware Corporation
(
Visual Quantify by Rational Software
(
Segue Solutions' SilkPerformer
(
Metamata Debugger ( (some people
list this as a profiler, though it looks like a plain debugger to me)
<b>15.5 Optimizers </b>
PreEmptive's DashO optimizer ()
TowerJ environment (compiler & runtime) from Tower Technology Corporation
TowerJ review: (
JOVE (
Condensity from Plumb Design ()
High Performance Compiler for Java from IBM alphaWorks
(
JAX size optimizer from IBM alphaWorks (
jres resource manager and compressor from IBM alphaWorks
(
Jshrink size optimizer from Eastridge Technology (
SourceGuard ()
Our look is the result of reader comments, our own experimentation, and feedback from distribution
channels. Distinctive covers complement our distinctive approach to technical topics, breathing
personality and life into potentially dry subjects.
The animal on the cover of Java™ Performance Tuning is a serval.
Emily Quill was the production editor and proofreader for Java™ Performance Tuning. Mary Anne
Weeks Mayo was the copyeditor for the book. Jane Ellin and Nancy Kotary performed quality
control reviews. Nancy Williams provided production assistance. Nancy Crumpton wrote the index.
This colophon was written by Emily Quill.
Hanna Dyer designed the cover of this book, based on a series design by Edie Freedman. The image
of the stopwatch is from the Stock Options photo collection. It was manipulated in Adobe