Java Performance: The Definitive Guide

Book description

Coding and testing are often considered separate areas of expertise. In this comprehensive guide, author and Java expert Scott Oaks takes the approach that anyone who works with Java should be equally adept at understanding how code behaves in the JVM, as well as the tunings likely to help its performance.

You’ll gain in-depth knowledge of Java application performance, using the Java Virtual Machine (JVM) and the Java platform, including the language and API. Developers and performance engineers alike will learn a variety of features, tools, and processes for improving the way Java 7 and 8 applications perform.

  • Apply four principles for obtaining the best results from performance testing
  • Use JDK tools to collect data on how a Java application is performing
  • Understand the advantages and disadvantages of using a JIT compiler
  • Tune JVM garbage collectors to affect programs as little as possible
  • Use techniques to manage heap memory and JVM native memory
  • Maximize Java threading and synchronization performance features
  • Tackle performance issues in Java EE and Java SE APIs
  • Improve Java-driven database application performance

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Should (and Shouldn’t) Read This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Safari® Books Online
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Introduction
    1. A Brief Outline
    2. Platforms and Conventions
      1. JVM Tuning Flags
    3. The Complete Performance Story
      1. Write Better Algorithms
      2. Write Less Code
      3. Oh Go Ahead, Prematurely Optimize
      4. Look Elsewhere: The Database Is Always the Bottleneck
      5. Optimize for the Common Case
    4. Summary
  3. 2. An Approach to Performance Testing
    1. Test a Real Application
      1. Microbenchmarks
        1. Microbenchmarks must use their results
        2. Microbenchmarks must not include extraneous operations
        3. Microbenchmarks must measure the correct input
      2. Macrobenchmarks
      3. Mesobenchmarks
      4. Common Code Examples
    2. Understand Throughput, Batching, and Response Time
      1. Elapsed Time (Batch) Measurements
      2. Throughput Measurements
      3. Response Time Tests
    3. Understand Variability
    4. Test Early, Test Often
    5. Summary
  4. 3. A Java Performance Toolbox
    1. Operating System Tools and Analysis
      1. CPU Usage
        1. Java and single-CPU usage
        2. Java and multi-CPU usage
      2. The CPU Run Queue
      3. Disk Usage
      4. Network Usage
    2. Java Monitoring Tools
      1. Basic VM Information
        1. Working with tuning flags
      2. Thread Information
      3. Class Information
      4. Live GC Analysis
      5. Heap Dump Postprocessing
    3. Profiling Tools
      1. Sampling Profilers
      2. Instrumented Profilers
      3. Blocking Methods and Thread Timelines
      4. Native Profilers
    4. Java Mission Control
      1. Java Flight Recorder
        1. JFR overview
        2. JFR Memory view
        3. JFR Code view
        4. Overview of JFR events
      2. Enabling JFR
        1. Enabling JFR via Java Mission Control
        2. Enabling JFR via the command line
      3. Selecting JFR Events
    5. Summary
  5. 4. Working with the JIT Compiler
    1. Just-in-Time Compilers: An Overview
      1. Hot Spot Compilation
    2. Basic Tunings: Client or Server (or Both)
      1. Optimizing Startup
      2. Optimizing Batch Operations
      3. Optimizing Long-Running Applications
    3. Java and JIT Compiler Versions
    4. Intermediate Tunings for the Compiler
      1. Tuning the Code Cache
      2. Compilation Thresholds
      3. Inspecting the Compilation Process
    5. Advanced Compiler Tunings
      1. Compilation Threads
      2. Inlining
      3. Escape Analysis
    6. Deoptimization
      1. Not Entrant Code
      2. Deoptimizing Zombie Code
    7. Tiered Compilation Levels
    8. Summary
  6. 5. An Introduction to Garbage Collection
    1. Garbage Collection Overview
      1. Generational Garbage Collectors
      2. GC Algorithms
        1. The serial garbage collector
        2. The throughput collector
        3. The CMS collector
        4. The G1 collector
      3. Choosing a GC Algorithm
        1. GC algorithms and batch jobs
        2. GC algorithms and throughput tests
        3. GC algorithms and response time tests
        4. Choosing between CMS and G1
    2. Basic GC Tuning
      1. Sizing the Heap
      2. Sizing the Generations
      3. Sizing Permgen and Metaspace
      4. Controlling Parallelism
      5. Adaptive Sizing
    3. GC Tools
    4. Summary
  7. 6. Garbage Collection Algorithms
    1. Understanding the Throughput Collector
      1. Adaptive and Static Heap Size Tuning
    2. Understanding the CMS Collector
      1. Tuning to Solve Concurrent Mode Failures
        1. Running the background thread more often
        2. Adjusting the CMS background threads
      2. Tuning CMS for Permgen
      3. Incremental CMS
    3. Understanding the G1 Collector
      1. Tuning G1
        1. Tuning the G1 background threads
        2. Tuning G1 to run more (or less) frequently
        3. Tuning G1 mixed GC cycles
    4. Advanced Tunings
      1. Tenuring and Survivor Spaces
      2. Allocating Large Objects
        1. Thread-local allocation buffers
        2. Sizing TLABs
        3. Humongous objects
        4. G1 region sizes
        5. G1 allocation of humongous objects
      3. AggressiveHeap
      4. Full Control Over Heap Size
    5. Summary
  8. 7. Heap Memory Best Practices
    1. Heap Analysis
      1. Heap Histograms
      2. Heap Dumps
      3. Out of Memory Errors
        1. Out of native memory
        2. Out of permgen or metaspace memory
        3. Out of heap memory
        4. GC overhead limit reached
    2. Using Less Memory
      1. Reducing Object Size
      2. Lazy Initialization
        1. Eager deinitialization
      3. Immutable and Canonical Objects
        1. Creating canonical objects
      4. String Interning
    3. Object Lifecycle Management
      1. Object Reuse
        1. Object pools
        2. Thread-local variables
      2. Weak, Soft, and Other References
        1. Soft references
        2. Weak references
        3. Finalizers and final references
    4. Summary
  9. 8. Native Memory Best Practices
    1. Footprint
      1. Measuring Footprint
      2. Minimizing Footprint
      3. Native NIO Buffers
      4. Native Memory Tracking
        1. NMT over time
    2. JVM Tunings for the Operating System
      1. Large Pages
        1. Linux huge (large) pages
        2. Linux transparent huge pages
        3. Windows large pages
        4. Large page sizes
      2. Compressed oops
    3. Summary
  10. 9. Threading and Synchronization Performance
    1. Thread Pools and ThreadPoolExecutors
      1. Setting the Maximum Number of Threads
      2. Setting the Minimum Number of Threads
      3. Thread Pool Task Sizes
      4. Sizing a ThreadPoolExecutor
    2. The ForkJoinPool
      1. Automatic Parallelization
    3. Thread Synchronization
      1. Costs of Synchronization
        1. Synchronization and scalability
        2. Costs of locking objects
      2. Avoiding Synchronization
      3. False Sharing
    4. JVM Thread Tunings
      1. Tuning Thread Stack Sizes
      2. Biased Locking
      3. Lock Spinning
      4. Thread Priorities
    5. Monitoring Threads and Locks
      1. Thread Visibility
      2. Blocked Thread Visibility
        1. Blocked threads and JFR
        2. Blocked threads and JStack
    6. Summary
  11. 10. Java Enterprise Edition Performance
    1. Basic Web Container Performance
      1. HTTP Session State
        1. HTTP session state memory
        2. Highly available HTTP session state
    2. Thread Pools
    3. Enterprise Java Session Beans
      1. Tuning EJB Pools
      2. Tuning EJB Caches
      3. Local and Remote Instances
    4. XML and JSON Processing
      1. Data Size
      2. An Overview of Parsing and Marshalling
      3. Choosing a Parser
        1. Pull parsers
        2. Push parsers (SAX)
        3. Alternate parsing implementations and parser factories
      4. XML Validation
      5. Document Models
      6. Java Object Models
    5. Object Serialization
      1. Transient Fields
      2. Overriding Default Serialization
      3. Compressing Serialized Data
      4. Keeping Track of Duplicate Objects
    6. Java EE Networking APIs
      1. Sizing Data Transfers
    7. Summary
  12. 11. Database Performance Best Practices
    1. JDBC
      1. JDBC Drivers
        1. Where work is performed
        2. The JDBC driver type
      2. Prepared Statements and Statement Pooling
        1. Setting up the statement pool
        2. Managing statement pools
      3. JDBC Connection Pools
      4. Transactions
        1. JDBC transaction control
        2. Transaction isolation and locking
      5. Result Set Processing
    2. JPA
      1. Transaction Handling
      2. Optimizing JPA Writes
      3. Optimizing JPA Reads
        1. Reading less data
        2. Using JOIN in queries
        3. Batching and queries
      4. JPA Caching
        1. Default caching (lazy loading)
        2. Caching and eager loading
        3. Join fetch and caching
        4. Avoiding queries
        5. Sizing the JPA cache
      5. JPA Read-Only Entities
    3. Summary
  13. 12. Java SE API Tips
    1. Buffered I/O
    2. Classloading
    3. Random Numbers
    4. Java Native Interface
    5. Exceptions
    6. String Performance
    7. Logging
    8. Java Collections API
      1. Synchronized Versus Unsynchronized
      2. Collection Sizing
      3. Collections and Memory Efficiency
    9. AggressiveOpts
      1. Alternate Implementations
      2. Miscellaneous Flags
    10. Lambdas and Anonymous Classes
      1. Lambda and Anonymous Classloading
    11. Stream and Filter Performance
      1. Lazy Traversal
    12. Summary
  14. A. Summary of Tuning Flags
  15. Index
  16. About the Author
  17. Colophon
  18. Copyright

Product information

  • Title: Java Performance: The Definitive Guide
  • Author(s): Scott Oaks
  • Release date: April 2014
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449358457