Java Performance Tuning, 2nd Edition

Book description

Performance has been an important issue for Java developers ever since the first version hit the streets. Over the years, Java performance has improved dramatically, but tuning is essential to get the best results, especially for J2EE applications. You can never have code that runs too fast.Java Peformance Tuning, 2nd edition provides a comprehensive and indispensable guide to eliminating all types of performance problems. Using many real-life examples to work through the tuning process in detail, JPT shows how tricks such as minimizing object creation and replacing strings with arrays can really pay off in improving your code's performance.Tuning J2EE applications bears many similarities to tuning J2SE apps, but important and specific considerations apply. For this reason, Java Performance Tuning, Second Edition includes four new chapters: a new chapter on J2EE application tuning in general followed by chapters on tuning JDBC, servlets and JSPs, and EJBs.Java Performance Tuning offers common-sense advice about what to tune and what to leave alone, emphasizing techniques that provide big performance gains with minimal code restructuring. It gives you crucial guidance that helps you tune without destroying your program's architecture. Blindly changing things in an effort to make a program run faster is a great way to create buggy, unmaintainable code. Java Performance Tuning teaches you to work efficiently and effectively, resulting in code that is robust, maintainable, and fast.Java Performance Tuning, Second Edition shows you how to:

  • Create a performance plan
  • Glean information about your program's behavior from profiling tools
  • Identify bottlenecks before tuning
  • Minimize the number of objects your program creates, particularly critical for J2EE applications
  • Optimize the use of strings
  • Avoid performance penalties from inefficient code
  • Improve the behavior of loops and switches
  • Optimize I/O behavior
  • Use appropriate algorithms for sorting and other common tasks
  • Use threads effectively
  • Optimize the performance of distributed systems
  • Speed up servlets and JSPs
  • Structure JDBC usage efficiently
  • Use effective design patterns to optimize EJB performance
Covers JDK 1.4

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Copyright
  3. Preface
    1. Contents of This Book
    2. New in the Second Edition
    3. Virtual Machine (VM) Versions
    4. Conventions Used in This Book
    5. Comments and Questions
    6. Acknowledgments
  4. 1. Introduction
    1. 1.1. Why Is It Slow?
    2. 1.2. The Tuning Game
    3. 1.3. System Limitations and What to Tune
    4. 1.4. A Tuning Strategy
    5. 1.5. Perceived Performance
      1. 1.5.1. Threading to Appear Quicker
      2. 1.5.2. Streaming to Appear Quicker
      3. 1.5.3. Caching to Appear Quicker
    6. 1.6. Starting to Tune
      1. 1.6.1. User Agreements
      2. 1.6.2. Setting Benchmarks
      3. 1.6.3. The Benchmark Harness
      4. 1.6.4. Taking Measurements
    7. 1.7. What to Measure
    8. 1.8. Don’t Tune What You Don’t Need to Tune
    9. 1.9. Performance Checklist
  5. 2. Profiling Tools
    1. 2.1. Measurements and Timings
    2. 2.2. Garbage Collection
      1. 2.2.1. Using -verbosegc to Analyze Garbage Collection
      2. 2.2.2. Tools for Analyzing -verbosegc Output
    3. 2.3. Method Calls
      1. 2.3.1. Profiling Methodology
      2. 2.3.2. Java 2 cpu=samples Profile Output
      3. 2.3.3. HotSpot and 1.3 -Xprof Profile Output
      4. 2.3.4. JDK 1.1.x -prof and Java 2 cpu=old Profile Output
      5. 2.3.5. Java 2 -Xhprof Option
      6. 2.3.6. Java 2 -Xaprof option
    4. 2.4. Object-Creation Profiling
    5. 2.5. Monitoring Gross Memory Usage
    6. 2.6. Client/Server Communications
      1. 2.6.1. Replacing Sockets
    7. 2.7. Performance Checklist
  6. 3. Underlying JDK Improvements
    1. 3.1. Garbage Collection
    2. 3.2. Tuning the Heap
    3. 3.3. Gross Tuning
      1. 3.3.1. Problems with a Larger Heap
      2. 3.3.2. Starting Versus Maximum Heap Size
      3. 3.3.3. Benchmarking Considerations
    4. 3.4. Fine-Tuning the Heap
      1. 3.4.1. Expanding the Heap
      2. 3.4.2. Minimizing Pauses
        1. 3.4.2.1. Incremental or “train” GC
        2. 3.4.2.2. Concurrent GC
        3. 3.4.2.3. Enlarge the “new” space
      3. 3.4.3. Disabling System.gc( ) Calls
      4. 3.4.4. Tuning RMI Garbage Collection
      5. 3.4.5. Extreme Heap and Intimate Shared Memory
      6. 3.4.6. Loading a Huge Number of Classes
      7. 3.4.7. Per-Thread Stack Size
      8. 3.4.8. Eliminate Finalizers
    5. 3.5. Sharing Memory
    6. 3.6. Replacing JDK Classes
    7. 3.7. Faster VMs
      1. 3.7.1. VM Speed Variations
      2. 3.7.2. VMs with JIT Compilers
      3. 3.7.3. VM Startup Time
      4. 3.7.4. Other VM Optimizations
    8. 3.8. Better Optimizing Compilers
      1. 3.8.1. What Optimizing Compilers Cannot Do
      2. 3.8.2. What Optimizing Compilers Can Do
        1. 3.8.2.1. Remove unused methods and classes
        2. 3.8.2.2. Increase statically bound calls
        3. 3.8.2.3. Cut dead code and unnecessary instructions, including checks for null
        4. 3.8.2.4. Use computationally cheaper alternatives (strength reduction)
        5. 3.8.2.5. Replace runtime computations with compiled results
        6. 3.8.2.6. Remove unused fields
        7. 3.8.2.7. Remove unnecessary parts of compiled files
        8. 3.8.2.8. Reduce necessary parts of compiled files
        9. 3.8.2.9. Alter access control to speed up invocations
        10. 3.8.2.10. Inline calls
        11. 3.8.2.11. Remove dynamic type checks
        12. 3.8.2.12. Unroll loops
        13. 3.8.2.13. Code motion
        14. 3.8.2.14. Eliminate common subexpressions
        15. 3.8.2.15. Eliminate unnecessary assignments
        16. 3.8.2.16. Rename classes, fields, and methods
        17. 3.8.2.17. Reorder or change bytecodes
        18. 3.8.2.18. Generate information to help a VM
      3. 3.8.3. Managing Compilers
    9. 3.9. Sun’s Compiler and Runtime Optimizations
      1. 3.9.1. Optimizations You Get for Free
        1. 3.9.1.1. Literal constants are folded
        2. 3.9.1.2. String concatenation is sometimes folded
        3. 3.9.1.3. Constant fields are inlined
        4. 3.9.1.4. Dead code branches are eliminated
      2. 3.9.2. Optimizations Performed When Using the -O Option
      3. 3.9.3. Performance Effects From Runtime Options
    10. 3.10. Compile to Native Machine Code
    11. 3.11. Native Method Calls
    12. 3.12. Uncompressed ZIP/JAR Files
    13. 3.13. Performance Checklist
  7. 4. Object Creation
    1. 4.1. Object-Creation Statistics
    2. 4.2. Object Reuse
      1. 4.2.1. Pool Management
      2. 4.2.2. ThreadLocals
      3. 4.2.3. Reusable Parameters
      4. 4.2.4. Canonicalizing Objects
        1. 4.2.4.1. String canonicalization
        2. 4.2.4.2. Changeable objects
        3. 4.2.4.3. Weak references
        4. 4.2.4.4. Enumerating constants
    3. 4.3. Reference Objects
      1. 4.3.1. Reference Types
      2. 4.3.2. SoftReference Flushing
      3. 4.3.3. The WeakHashMap Class
        1. 4.3.3.1. How WeakHashMap works
        2. 4.3.3.2. Some consequences of the WeakHashMap implementation
    4. 4.4. Avoiding Garbage Collection
    5. 4.5. Initialization
    6. 4.6. Early and Late Initialization
      1. 4.6.1. Preallocating Objects
      2. 4.6.2. Lazy Initialization
    7. 4.7. Performance Checklist
  8. 5. Strings
    1. 5.1. The Performance Effects of Strings
    2. 5.2. Compile-Time Versus Runtime Resolution of Strings
    3. 5.3. Conversions to Strings
      1. 5.3.1. Converting longs to Strings
      2. 5.3.2. Converting ints to Strings
      3. 5.3.3. Converting bytes, shorts, chars, and booleans to Strings
      4. 5.3.4. Converting floats to Strings
      5. 5.3.5. Converting doubles to Strings
      6. 5.3.6. Converting Objects to Strings
    4. 5.4. Strings Versus char Arrays
      1. 5.4.1. Word-Counting Example
      2. 5.4.2. Line Filter Example
      3. 5.4.3. Line Filtering with Regular Expressions
    5. 5.5. String Comparisons and Searches
    6. 5.6. Sorting Internationalized Strings
    7. 5.7. Performance Checklist
  9. 6. Exceptions, Assertions, Casts, and Variables
    1. 6.1. Exceptions
      1. 6.1.1. The Cost of try-catch Blocks Without an Exception
      2. 6.1.2. The Cost of try-catch Blocks with an Exception
      3. 6.1.3. Using Exceptions Without the Stack Trace Overhead
      4. 6.1.4. Conditional Error Checking
    2. 6.2. Assertions
      1. 6.2.1. Assertion Overhead
    3. 6.3. Casts
    4. 6.4. Variables
    5. 6.5. Method Parameters
    6. 6.6. Performance Checklist
  10. 7. Loops, Switches, and Recursion
    1. 7.1. Loops
      1. 7.1.1. Move Code Out of the Loop
      2. 7.1.2. Use Temporary Variables
      3. 7.1.3. Don’t Terminate Loops with Method Calls
      4. 7.1.4. Use int for Index Variables
      5. 7.1.5. Use System.arraycopy( )
      6. 7.1.6. Use Efficient Comparisons
      7. 7.1.7. Put the Most Common Case First
      8. 7.1.8. Avoid Reflection
    2. 7.2. Tuning a Loop
    3. 7.3. Exception-Terminated Loops
    4. 7.4. Switches
    5. 7.5. Recursion
    6. 7.6. Recursion and Stacks
    7. 7.7. Performance Checklist
  11. 8. I/O, Logging, and Console Output
    1. 8.1. Replacing System.out
    2. 8.2. Logging
    3. 8.3. From Raw I/O to Smokin’ I/O
    4. 8.4. Serialization
    5. 8.5. Clustering Objects and Counting I/O Operations
    6. 8.6. Compression
    7. 8.7. NIO
      1. 8.7.1. Connecting Client Sockets
      2. 8.7.2. Nondirect Buffers: Fast Casts for Primitive Arrays
      3. 8.7.3. Direct Buffers: Optimized I/O Operations
      4. 8.7.4. Multiplexing
    8. 8.8. Performance Checklist
  12. 9. Sorting
    1. 9.1. Avoiding Unnecessary Sorting Overhead
    2. 9.2. An Efficient Sorting Framework
    3. 9.3. Better Than O(nlogn) Sorting
    4. 9.4. Performance Checklist
  13. 10. Threading
    1. 10.1. User-Interface Thread and Other Threads
    2. 10.2. Race Conditions
    3. 10.3. Deadlocks
    4. 10.4. Synchronization Overhead
      1. 10.4.1. Desynchronization and Synchronized Wrappers
      2. 10.4.2. Avoiding Serialized Execution
    5. 10.5. Timing Multithreaded Tests
    6. 10.6. Atomic Access and Assignment
    7. 10.7. Thread Pools
    8. 10.8. Load Balancing
      1. 10.8.1. Free Load Balancing from TCP/IP
      2. 10.8.2. Load-Balancing Classes
      3. 10.8.3. A Load-Balancing Example
    9. 10.9. Threaded Problem-Solving Strategies
    10. 10.10. Performance Checklist
  14. 11. Appropriate Data Structures and Algorithms
    1. 11.1. Collections
    2. 11.2. Java 2 Collections
    3. 11.3. Hashtables and HashMaps
    4. 11.4. Optimizing Queries
      1. 11.4.1. The Query Problem
        1. 11.4.1.1. Applying the Boolean-OR operator optimization
        2. 11.4.1.2. Eliminating the unnecessarily repeated method call
        3. 11.4.1.3. Eliminating casts and extra access calls
        4. 11.4.1.4. Avoiding synchronization
        5. 11.4.1.5. Avoiding the method accessor
        6. 11.4.1.6. Tighter typing of the collection elements
        7. 11.4.1.7. Optimizing map queries
    5. 11.5. Comparing LinkedLists and ArrayLists
      1. 11.5.1. The Vector and ArrayList Implementations
      2. 11.5.2. The LinkedList Implementation
      3. 11.5.3. Performance Tests
      4. 11.5.4. Querying Performance
    6. 11.6. The RandomAccess Interface
      1. 11.6.1. What Does RandomAccess Mean?
      2. 11.6.2. How Is RandomAccess Used?
      3. 11.6.3. Speedup from RandomAccess
      4. 11.6.4. Forward and Backward Compatibility
    7. 11.7. Cached Access
    8. 11.8. Caching Examples
    9. 11.9. Finding the Index for Partially Matched Strings
    10. 11.10. Search Trees
    11. 11.11. Performance Checklist
  15. 12. Distributed Computing
    1. 12.1. Tools
    2. 12.2. Message Reduction
      1. 12.2.1. CORBA Example
      2. 12.2.2. RMI Example
      3. 12.2.3. Proprietary Communications Layer
    3. 12.3. Comparing Communications Layers
    4. 12.4. Caching
    5. 12.5. Batching I
    6. 12.6. Application Partitioning
    7. 12.7. Batching II
    8. 12.8. Low-Level Communication Optimizations
      1. 12.8.1. Compression
      2. 12.8.2. Caching
      3. 12.8.3. Transfer Batching
      4. 12.8.4. Multiplexing
    9. 12.9. Distributed Garbage Collection
    10. 12.10. Databases
    11. 12.11. Web Services
      1. 12.11.1. Measuring Web Services Performance
        1. 12.11.1.1. Measuring server-side method execution time
        2. 12.11.1.2. Measuring the full round-trip time
        3. 12.11.1.3. Inferring round-trip overhead
        4. 12.11.1.4. Inferring network communication time
        5. 12.11.1.5. Inferring DNS lookup time
        6. 12.11.1.6. Inferring marshalling time
      2. 12.11.2. High-Performance Web Services
        1. 12.11.2.1. Service granularity
        2. 12.11.2.2. Load balancing
        3. 12.11.2.3. Asynchronous processing
    12. 12.12. Performance Checklist
  16. 13. When to Optimize
    1. 13.1. When Not to Optimize
    2. 13.2. Tuning Class Libraries and Beans
    3. 13.3. Analysis
    4. 13.4. Design and Architecture
      1. 13.4.1. Shared Resources
        1. 13.4.1.1. Transactions
        2. 13.4.1.2. Locking
        3. 13.4.1.3. Parallelism
        4. 13.4.1.4. Data parallelism
      2. 13.4.2. Scaling
      3. 13.4.3. Distributed Applications
      4. 13.4.4. Object Design
      5. 13.4.5. Techniques for Predicting Performance
        1. 13.4.5.1. Factor in comparative performance of operations
        2. 13.4.5.2. Consider the relative costs of different types of accesses and updates
        3. 13.4.5.3. Use simulations and benchmarks
        4. 13.4.5.4. Consider the total work done and the design overhead
        5. 13.4.5.5. Focus on shared resources
        6. 13.4.5.6. Predict the effects of parallelism
        7. 13.4.5.7. Assess the costs of data conversions
        8. 13.4.5.8. Determine whether batch processing is faster
    5. 13.5. Tuning After Deployment
    6. 13.6. More Factors That Affect Performance
      1. 13.6.1. User Interface Usability
      2. 13.6.2. Training
      3. 13.6.3. Server Downtime
    7. 13.7. Performance Planning
    8. 13.8. Performance Checklist
  17. 14. Underlying Operating System and Network Improvements
    1. 14.1. Hard Disks
      1. 14.1.1. Disk I/O
      2. 14.1.2. Clustering Files
      3. 14.1.3. Cached Filesystems (RAM Disks, tmpfs, cachefs)
      4. 14.1.4. Disk Fragmentation
      5. 14.1.5. Disk Sweet Spots
    2. 14.2. CPU
      1. 14.2.1. CPU Load
      2. 14.2.2. Process Priorities
    3. 14.3. RAM
    4. 14.4. Network I/O
      1. 14.4.1. Latency
      2. 14.4.2. TCP/IP Stacks
      3. 14.4.3. Network Bottlenecks
      4. 14.4.4. DNS Lookups
    5. 14.5. Performance Checklist
  18. 15. J2EE Performance Tuning
    1. 15.1. Performance Planning
    2. 15.2. J2EE Monitoring and Profiling Tools
      1. 15.2.1. Features to Look For
      2. 15.2.2. J2EE Monitoring Tools
    3. 15.3. Measurements: What, Where, and How
      1. 15.3.1. An Instructive Analysis
      2. 15.3.2. Suggested Measurements
      3. 15.3.3. Symptoms of Performance Problems
      4. 15.3.4. Useful Statistics to Analyze from Measurements
    4. 15.4. Load Testing
    5. 15.5. User Perception
      1. 15.5.1. Screen Navigation
      2. 15.5.2. Page Display
      3. 15.5.3. Priority Service
      4. 15.5.4. Internet Congestion
      5. 15.5.5. Communication Failures
      6. 15.5.6. Good Peak Performance
      7. 15.5.7. Cumulative Delay and User Impatience
      8. 15.5.8. User Bandwidth
    6. 15.6. Clustering and Load Balancing
      1. 15.6.1. Load-Balancing Mechanisms
        1. 15.6.1.1. DNS load balancing
        2. 15.6.1.2. Hardware load balancer
        3. 15.6.1.3. Load-balancing dispatcher/Proxy load balancing
        4. 15.6.1.4. URL-based load balancing
        5. 15.6.1.5. Server-pooled objects
        6. 15.6.1.6. Client-based load balancing
        7. 15.6.1.7. Application configuration load balancing
        8. 15.6.1.8. Database partitioning
      2. 15.6.2. Load-Balancing Algorithms
    7. 15.7. Tuning JMS
    8. 15.8. Performance Checklist
  19. 16. Tuning JDBC
    1. 16.1. Measuring JDBC Performance
      1. 16.1.1. The Core java.sql Interfaces
      2. 16.1.2. Wrapping Classes Using Proxy Objects
        1. 16.1.2.1. Wrapping the Connection class
        2. 16.1.2.2. Wrapping the Statement classes
        3. 16.1.2.3. Wrapping the ResultSet class
        4. 16.1.2.4. The JDBC wrapper framework
        5. 16.1.2.5. Using the JDBC wrapper framework
    2. 16.2. Tuning JDBC
      1. 16.2.1. JDBC Drivers
        1. 16.2.1.1. Type 1: JDBC-bridging driver
        2. 16.2.1.2. Type 2: Native API, part-Java driver
        3. 16.2.1.3. Type 3: Pure Java driver for database middleware
        4. 16.2.1.4. Type 4: Pure Java driver direct to a database
      2. 16.2.2. Connection Pooling
      3. 16.2.3. Optimizing SQL
      4. 16.2.4. Set-Based Processing
      5. 16.2.5. Database Server-Side Processing
      6. 16.2.6. Minimizing Transferred Data
      7. 16.2.7. Caching
      8. 16.2.8. Prepared Statements
      9. 16.2.9. Batching
        1. 16.2.9.1. Batching accesses by optimizing row fetching
        2. 16.2.9.2. Batching updates
      10. 16.2.10. Data Structures
      11. 16.2.11. Minimizing Data-Conversion Overhead
      12. 16.2.12. Metadata
      13. 16.2.13. Handling Exceptions
      14. 16.2.14. Stored Procedures
      15. 16.2.15. Transaction Optimization
        1. 16.2.15.1. Auto-commit mode
        2. 16.2.15.2. Transaction levels
        3. 16.2.15.3. User-controlled transactions
        4. 16.2.15.4. Savepoints and distributed transactions
      16. 16.2.16. Database Location
    3. 16.3. Performance Checklist
  20. 17. Tuning Servlets and JSPs
    1. 17.1. Don’t Use SingleThreadModel
    2. 17.2. Efficient Page Creation and Output
      1. 17.2.1. Minimize Output and Logging
      2. 17.2.2. Use Strings Efficiently
      3. 17.2.3. Use Bytes Versus Chars
      4. 17.2.4. Use Network Buffers Efficiently
      5. 17.2.5. Display Static Pages Quickly
      6. 17.2.6. Optimize Data Conversions
      7. 17.2.7. Use ServletOutputStream Directly (Servlets Only)
      8. 17.2.8. Optimize Partial Page Display
    3. 17.3. Body Tags
      1. 17.3.1. Use Include Directive, Not Action
      2. 17.3.2. Minimize Scope of useBean Action
      3. 17.3.3. Minimize Custom Tags
      4. 17.3.4. Use Redirects Versus Forwards
    4. 17.4. Cache Tags
    5. 17.5. HttpSession
      1. 17.5.1. Timing Out Sessions
      2. 17.5.2. HttpSession Versus Stateful Session Beans
      3. 17.5.3. HttpSession Serialization
      4. 17.5.4. Distributing Sessions for Higher Scalability
      5. 17.5.5. Efficient Resource Management
    6. 17.6. Compression
    7. 17.7. More Performance Tips
    8. 17.8. Case Study: Ace’s Hardware SPECmine Tool
    9. 17.9. Performance Checklist
  21. 18. Tuning EJBs
    1. 18.1. Primary Design Guidelines
      1. 18.1.1. Coarse-Grained EJBs Are Faster
      2. 18.1.2. EJBs Are Not Data Wrappers
      3. 18.1.3. Read-Only Data Is Different
      4. 18.1.4. Stateless Session Beans Are Faster
      5. 18.1.5. Cache JNDI Lookups
      6. 18.1.6. CMP or BMP?
      7. 18.1.7. EJB Transactions
    2. 18.2. Performance-Optimizing Design Patterns
      1. 18.2.1. Reducing the Number of Network Trips: The Value Object Pattern
      2. 18.2.2. Optimizing Database Access: The Data Access Object Pattern and the Fast Lane Reader Pattern
      3. 18.2.3. Efficiently Transferring Large Datasets: The Page-by-Page Iterator Pattern and the ValueListHandler Pattern
      4. 18.2.4. Caching Services: The Service Locator, Verified Service Locator, and EJBHomeFactory Patterns
      5. 18.2.5. Combining EJBs: The Session Façade and CompositeEntity Patterns
      6. 18.2.6. Reusing Objects: The Factory and Builder Patterns
      7. 18.2.7. Reducing Locking Conflicts: The Optimistic Locking Pattern
      8. 18.2.8. Load Balancing: The Reactor and Front Controller Patterns
      9. 18.2.9. Optimized Message Handling: The Proxy and Decorator Patterns
      10. 18.2.10. Optimizing CPU Usage: The Message Façade Pattern
    3. 18.3. The Application Server
      1. 18.3.1. Security Layer
      2. 18.3.2. Gross Configuration
      3. 18.3.3. Tuning Application Servers
    4. 18.4. More Suggestions for Tuning EJBs
    5. 18.5. Case Study: The Pet Store
    6. 18.6. Case Study: Elite.com
    7. 18.7. Performance Checklist
  22. 19. Further Resources
    1. 19.1. The Primary Resource
    2. 19.2. Books
    3. 19.3. Magazines
    4. 19.4. URLs
    5. 19.5. Profilers
    6. 19.6. Optimizers
  23. About the Author
  24. Colophon
  25. Copyright

Product information

  • Title: Java Performance Tuning, 2nd Edition
  • Author(s): Jack Shirazi
  • Release date: January 2003
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596003777