Cover image for Java Performance Tuning

Book description

No matter what language they're programming in, developers always wish things would run faster! Especially when writing mission-critical applications, no one wants to be limited by the programming environment. Java Performance Tuning provides all the details you need to know to "performance tune" any type of Java program and make Java code run significantly faster. Java Performance Tuning contains step-by-step instructions on all aspects of the performance tuning process, right from such early considerations as setting goals, measuring performance, and choosing a compiler. Extensive examples for tuning many parts of an application are described in detail, and any pitfalls are identified. The book also provides performance tuning checklists that enable developers to make their tuning as comprehensive as possible. Java Performance Tuning is a book to help you "roll up your sleeves" and examine your code in detail in order to apply different tuning techniques to produce the best possible result. The book shows you how to:

  • Create and plan a tuning strategy

  • Avoid performance penalties from inefficient code

  • Minimize the number of objects a program creates

  • Improve the behavior of loops and switches

  • Use threads effectively

Table of Contents

  1. Java Performance Tuning
    1. Preface
      1. Contents of This Book
      2. Virtual Machine (VM) Versions
      3. Conventions Used in This Book
      4. Comments and Questions
      5. Acknowledgments
    2. 1. Introduction
      1. Why Is It Slow?
      2. The Tuning Game
      3. System Limitations and What to Tune
      4. A Tuning Strategy
      5. Perceived Performance
        1. Threading to Appear Quicker
        2. Streaming to Appear Quicker
        3. Caching to Appear Quicker
      6. Starting to Tune
        1. User Agreements
        2. Setting Benchmarks
        3. The Benchmark Harness
        4. Taking Measurements
      7. What to Measure
      8. Don’t Tune What You Don’t Need to Tune
      9. Performance Checklist
    3. 2. Profiling Tools
      1. Measurements and Timings
      2. Garbage Collection
      3. Method Calls
        1. Profiling Methodology
        2. Java 2 “cpu=samples” Profile Output
        3. HotSpot and 1.3 “-Xprof” Profile Output
        4. JDK 1.1.x “-prof” and Java 2 “cpu=old” Profile Output
      4. Object-Creation Profiling
      5. Monitoring Gross Memory Usage
      6. Client/Server Communications
        1. Replacing Sockets
      7. Performance Checklist
    4. 3. Underlying JDK Improvements
      1. Garbage Collection
      2. Replacing JDK Classes
      3. Faster VMs
        1. VM Speed Variations
        2. VMs with JIT Compilers
        3. VM Startup Time
        4. Other VM Optimizations
      4. Better Optimizing Compilers
        1. What Optimizing Compilers Cannot Do
        2. What Optimizing Compilers Can Do
          1. Remove unused methods and classes
          2. Increase statically bound calls
          3. Cut dead code and unnecessary instructions, including checks for null
          4. Use computationally cheaper alternatives (strength reduction)
          5. Replace runtime computations with compiled results
          6. Remove unused fields
          7. Remove unnecessary parts of compiled files
          8. Reduce necessary parts of compiled files
          9. Alter access control to speed up invocations
          10. Inline calls
          11. Remove dynamic type checks
          12. Unroll loops
          13. Code motion
          14. Eliminate common subexpressions
          15. Eliminate unnecessary assignments
          16. Rename classes, fields, and methods
          17. Reorder or change bytecodes
          18. Generate information to help a VM
        3. Managing Compilers
      5. Sun’s Compiler and Runtime Optimizations
        1. Optimizations You Get for Free
          1. Literal constants are folded
          2. String concatenation is sometimes folded
          3. Constant fields are inlined
          4. Dead code branches are eliminated
        2. Optimizations Performed When Using the -O Option
        3. Performance Effects From Runtime Options
      6. Compile to Native Machine Code
      7. Native Method Calls
      8. Uncompressed ZIP/JAR Files
      9. Performance Checklist
    5. 4. Object Creation
      1. Object-Creation Statistics
      2. Object Reuse
        1. Pool Management
        2. ThreadLocals
        3. Reusable Parameters
        4. Canonicalizing Objects
          1. String canonicalization
          2. Changeable objects
          3. Weak references
          4. Enumerating constants
      3. Avoiding Garbage Collection
      4. Initialization
      5. Early and Late Initialization
        1. Preallocating Objects
        2. Lazy Initialization
      6. Performance Checklist
    6. 5. Strings
      1. The Performance Effects of Strings
      2. Compile-Time Versus Runtime Resolution of Strings
      3. Conversions to Strings
        1. Converting longs to Strings
        2. Converting ints to Strings
        3. Converting bytes, shorts, chars, and booleans to Strings
        4. Converting floats to Strings
        5. Converting doubles to Strings
        6. Converting Objects to Strings
      4. Strings Versus char Arrays
        1. Word-Counting Example
        2. Line Filter Example
      5. String Comparisons and Searches
      6. Sorting Internationalized Strings
      7. Performance Checklist
    7. 6. Exceptions, Casts, and Variables
      1. Exceptions
        1. The Cost of try-catch Blocks Without an Exception
        2. The Cost of try-catch Blocks with an Exception
        3. Using Exceptions Without the Stack Trace Overhead
        4. Conditional Error Checking
      2. Casts
      3. Variables
      4. Method Parameters
      5. Performance Checklist
    8. 7. Loops and Switches
      1. Java.io.Reader Converter
      2. Exception-Terminated Loops
      3. Switches
      4. Recursion
      5. Recursion and Stacks
      6. Performance Checklist
    9. 8. I/O, Logging, and Console Output
      1. Replacing System.out
      2. Logging
      3. From Raw I/O to Smokin’ I/O
      4. Serialization
      5. Clustering Objects and Counting I/O Operations
      6. Compression
      7. Performance Checklist
    10. 9. Sorting
      1. Avoiding Unnecessary Sorting Overhead
      2. An Efficient Sorting Framework
      3. Better Than O(nlogn) Sorting
      4. Performance Checklist
    11. 10. Threading
      1. User-Interface Thread and Other Threads
      2. Race Conditions
      3. Deadlocks
      4. Synchronization Overheads
        1. Desynchronization and Synchronized Wrappers
        2. Avoiding Serialized Execution
      5. Timing Multithreaded Tests
      6. Atomic Access and Assignment
      7. Thread Pools
      8. Load Balancing
        1. Free Load Balancing from TCP/IP
        2. Load-Balancing Classes
        3. A Load-Balancing Example
      9. Threaded Problem-Solving Strategies
      10. Performance Checklist
    12. 11. Appropriate Data Structures and Algorithms
      1. Collections
      2. Java 2 Collections
      3. Hashtables and HashMaps
      4. Cached Access
      5. Caching Example I
      6. Caching Example II
      7. Finding the Index for Partially Matched Strings
      8. Search Trees
      9. Performance Checklist
    13. 12. Distributed Computing
      1. Tools
      2. Message Reduction
        1. CORBA Example
        2. RMI Example
        3. Proprietary Communications Layer
      3. Comparing Communication Layers
      4. Caching
      5. Batching I
      6. Application Partitioning
      7. Batching II
      8. Low-Level Communication Optimizations
        1. Compression
        2. Caching
        3. Transfer Batching
        4. Multiplexing
      9. Distributed Garbage Collection
      10. Databases
      11. Performance Checklist
    14. 13. When to Optimize
      1. When Not to Optimize
      2. Tuning Class Libraries and Beans
      3. Analysis
      4. Design and Architecture
        1. Shared Resources
          1. Transactions
          2. Locking
          3. Parallelism
          4. Data parallelism
        2. Scaling
        3. Distributed Applications
        4. Object Design
        5. Techniques for Predicting Performance
          1. Factor in comparative performance of operations
          2. Consider the relative costs of different types of accesses and updates
          3. Use simulations and benchmarks
          4. Consider the total work done and the design overhead
          5. Focus on shared resources
          6. Predict the effects of parallelism
          7. Assess the costs of data conversions
          8. Determine whether batch processing is faster
      5. Tuning After Deployment
      6. More Factors That Affect Performance
        1. User Interface Usability
        2. Training
        3. Server Downtime
      7. Performance Checklist
    15. 14. Underlying Operating System and Network Improvements
      1. Hard Disks
        1. Disk I/O
        2. Clustering Files
        3. Cached Filesystems (RAM Disks, tmpfs, cachefs)
        4. Disk Fragmentation
        5. Disk Sweet Spots
      2. CPU
        1. CPU Load
        2. Process Priorities
      3. RAM
      4. Network I/O
        1. Latency
        2. TCP/IP Stacks
        3. Network Bottlenecks
        4. DNS Lookups
      5. Performance Checklist
    16. 15. Further Resources
      1. Books
      2. Magazines
      3. URLs
      4. Profilers
      5. Optimizers
    17. Index
    18. Colophon