Java Performance, 2nd Edition

Book description

Coding and testing are generally considered separate areas of expertise. In this practical book, Java expert Scott Oaks takes the approach that anyone who works with Java should be adept at understanding how code behaves in the Java Virtual Machine—including the tunings likely to help performance. This updated second edition helps you gain in-depth knowledge of Java application performance using both the JVM and the Java platform.

Developers and performance engineers alike will learn a variety of features, tools, and processes for improving the way the Java 8 and 11 LTS releases perform. While the emphasis is on production-supported releases and features, this book also features previews of exciting new technologies such as ahead-of-time compilation and experimental garbage collections.

  • Understand how various Java platforms and compilers affect performance
  • Learn how Java garbage collection works
  • Apply four principles to obtain best results from performance testing
  • Use the JDK and other tools to learn how a Java application is performing
  • Minimize the garbage collector’s impact through tuning and programming practices
  • Tackle performance issues in Java 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. What’s New in the Second Edition
    3. Conventions Used in This Book
    4. Using Code Examples
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  2. 1. Introduction
    1. A Brief Outline
    2. Platforms and Conventions
      1. Java Platforms
      2. Hardware Platforms
    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
      2. Macrobenchmarks
      3. Mesobenchmarks
    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. Benchmark Examples
      1. Java Microbenchmark Harness
      2. Common Code Examples
    6. Summary
  4. 3. A Java Performance Toolbox
    1. Operating System Tools and Analysis
      1. CPU Usage
      2. The CPU Run Queue
      3. Disk Usage
      4. Network Usage
    2. Java Monitoring Tools
      1. Basic VM Information
      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 Flight Recorder
      1. Java Mission Control
      2. JFR Overview
      3. Enabling JFR
      4. Selecting JFR Events
    5. Summary
  5. 4. Working with the JIT Compiler
    1. Just-in-Time Compilers: An Overview
      1. HotSpot Compilation
    2. Tiered Compilation
    3. Common Compiler Flags
      1. Tuning the Code Cache
      2. Inspecting the Compilation Process
      3. Tiered Compilation Levels
      4. Deoptimization
    4. Advanced Compiler Flags
      1. Compilation Thresholds
      2. Compilation Threads
      3. Inlining
      4. Escape Analysis
      5. CPU-Specific Code
    5. Tiered Compilation Trade-offs
    6. The GraalVM
    7. Precompilation
      1. Ahead-of-Time Compilation
      2. GraalVM Native Compilation
    8. Summary
  6. 5. An Introduction to Garbage Collection
    1. Garbage Collection Overview
      1. Generational Garbage Collectors
      2. GC Algorithms
      3. Choosing a GC Algorithm
    2. Basic GC Tuning
      1. Sizing the Heap
      2. Sizing the Generations
      3. Sizing Metaspace
      4. Controlling Parallelism
    3. GC Tools
      1. Enabling GC Logging in JDK 8
      2. Enabling GC Logging in JDK 11
    4. Summary
  7. 6. Garbage Collection Algorithms
    1. Understanding the Throughput Collector
      1. Adaptive and Static Heap Size Tuning
    2. Understanding the G1 Garbage Collector
      1. Tuning G1 GC
    3. Understanding the CMS Collector
      1. Tuning to Solve Concurrent Mode Failures
    4. Advanced Tunings
      1. Tenuring and Survivor Spaces
      2. Allocating Large Objects
      3. AggressiveHeap
      4. Full Control Over Heap Size
    5. Experimental GC Algorithms
      1. Concurrent Compaction: ZGC and Shenandoah
      2. No Collection: Epsilon GC
    6. Summary
  8. 7. Heap Memory Best Practices
    1. Heap Analysis
      1. Heap Histograms
      2. Heap Dumps
      3. Out-of-Memory Errors
    2. Using Less Memory
      1. Reducing Object Size
      2. Using Lazy Initialization
      3. Using Immutable and Canonical Objects
    3. Object Life-Cycle Management
      1. Object Reuse
      2. Soft, Weak, and Other References
      3. Compressed Oops
    4. Summary
  9. 8. Native Memory Best Practices
    1. Footprint
      1. Measuring Footprint
      2. Minimizing Footprint
      3. Native Memory Tracking
      4. Shared Library Native Memory
    2. JVM Tunings for the Operating System
      1. Large Pages
    3. Summary
  10. 9. Threading and Synchronization Performance
    1. Threading and Hardware
    2. 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
    3. The ForkJoinPool
      1. Work Stealing
      2. Automatic Parallelization
    4. Thread Synchronization
      1. Costs of Synchronization
      2. Avoiding Synchronization
      3. False Sharing
    5. JVM Thread Tunings
      1. Tuning Thread Stack Sizes
      2. Biased Locking
      3. Thread Priorities
    6. Monitoring Threads and Locks
      1. Thread Visibility
      2. Blocked Thread Visibility
    7. Summary
  11. 10. Java Servers
    1. Java NIO Overview
    2. Server Containers
      1. Tuning Server Thread Pools
      2. Async Rest Servers
    3. Asynchronous Outbound Calls
      1. Asynchronous HTTP
    4. JSON Processing
      1. An Overview of Parsing and Marshaling
      2. JSON Objects
      3. JSON Parsing
    5. Summary
  12. 11. Database Performance Best Practices
    1. Sample Database
    2. JDBC
      1. JDBC Drivers
      2. JDBC Connection Pools
      3. Prepared Statements and Statement Pooling
      4. Transactions
      5. Result Set Processing
    3. JPA
      1. Optimizing JPA Writes
      2. Optimizing JPA Reads
      3. JPA Caching
    4. Spring Data
    5. Summary
  13. 12. Java SE API Tips
    1. Strings
      1. Compact Strings
      2. Duplicate Strings and String Interning
      3. String Concatenation
    2. Buffered I/O
    3. Classloading
      1. Class Data Sharing
    4. Random Numbers
    5. Java Native Interface
    6. Exceptions
    7. Logging
    8. Java Collections API
      1. Synchronized Versus Unsynchronized
      2. Collection Sizing
      3. Collections and Memory Efficiency
    9. Lambdas and Anonymous Classes
    10. Stream and Filter Performance
      1. Lazy Traversal
    11. Object Serialization
      1. Transient Fields
      2. Overriding Default Serialization
      3. Compressing Serialized Data
      4. Keeping Track of Duplicate Objects
    12. Summary
  14. A. Summary of Tuning Flags
  15. Index

Product information

  • Title: Java Performance, 2nd Edition
  • Author(s): Scott Oaks
  • Release date: February 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492056119