Foreword
Preface
Conventions Used in This Book
Using Code Examples
O’Reilly Safari
How to Contact Us
Acknowledgments
1. Optimization and Performance Defined
Java Performance—The Wrong Way
Java Performance Overview
Performance as an Experimental Science
A Taxonomy for Performance
Throughput
Latency
Capacity
Utilization
Efficiency
Scalability
Degradation
Connections Between the Observables
Reading Performance Graphs
Summary
2. Overview of the JVM
Interpreting and Classloading
Executing Bytecode
Introducing HotSpot
Introducing Just-in-Time Compilation
JVM Memory Management
Threading and the Java Memory Model
Meet the JVMs
A Note on Licenses
Monitoring and Tooling for the JVM
VisualVM
Summary
3. Hardware and Operating Systems
Introduction to Modern Hardware
Memory
Memory Caches
Modern Processor Features
Translation Lookaside Buffer
Branch Prediction and Speculative Execution
Hardware Memory Models
Operating Systems
The Scheduler
A Question of Time
Context Switches
A Simple System Model
Basic Detection Strategies
Utilizing the CPU
Garbage Collection
I/O
Mechanical Sympathy
Virtualization
The JVM and the Operating System
Summary
4. Performance Testing Patterns and Antipatterns
Types of Performance Test
Latency Test
Throughput Test
Load Test
Stress Test
Endurance Test
Capacity Planning Test
Degradation Test
Best Practices Primer
Top-Down Performance
Creating a Test Environment
Identifying Performance Requirements
Java-Specific Issues
Performance Testing as Part of the SDLC
Introducing Performance Antipatterns
Boredom
Résumé Padding
Peer Pressure
Lack of Understanding
Misunderstood/Nonexistent Problem
Performance Antipatterns Catalogue
Distracted by Shiny
Distracted by Simple
Performance Tuning Wizard
Tuning by Folklore
The Blame Donkey
Missing the Bigger Picture
UAT Is My Desktop
Production-Like Data Is Hard
Cognitive Biases and Performance Testing
Reductionist Thinking
Confirmation Bias
Fog of War (Action Bias)
Risk Bias
Ellsberg’s Paradox
Summary
5. Microbenchmarking and Statistics
Introduction to Measuring Java Performance
Introduction to JMH
Don’t Microbenchmark If You Can Help It (A True Story)
Heuristics for When to Microbenchmark
The JMH Framework
Executing Benchmarks
Statistics for JVM Performance
Types of Error
Non-Normal Statistics
Interpretation of Statistics
Summary
6. Understanding Garbage Collection
Introducing Mark and Sweep
Garbage Collection Glossary
Introducing the HotSpot Runtime
Representing Objects at Runtime
GC Roots and Arenas
Allocation and Lifetime
Weak Generational Hypothesis
Garbage Collection in HotSpot
Thread-Local Allocation
Hemispheric Collection
The Parallel Collectors
Young Parallel Collections
Old Parallel Collections
Limitations of Parallel Collectors
The Role of Allocation
Summary
7. Advanced Garbage Collection
Tradeoffs and Pluggable Collectors
Concurrent GC Theory
JVM Safepoints
Tri-Color Marking
CMS
How CMS Works
Basic JVM Flags for CMS
G1
G1 Heap Layout and Regions
G1 Algorithm Design
G1 Phases
Basic JVM Flags for G1
Shenandoah
Concurrent Compaction
Obtaining Shenandoah
C4 (Azul Zing)
The Loaded Value Barrier
Balanced (IBM J9)
J9 Object Headers
Large Arrays in Balanced
NUMA and Balanced
Legacy HotSpot Collectors
Serial and SerialOld
Incremental CMS (iCMS)
Deprecated and Removed GC Combinations
Epsilon
Summary
8. GC Logging, Monitoring, Tuning, and Tools
Introduction to GC Logging
Switching On GC Logging
GC Logs Versus JMX
Drawbacks of JMX
Benefits of GC Log Data
Log Parsing Tools
Censum
GCViewer
Different Visualizations of the Same Data
Basic GC Tuning
Understanding Allocation
Understanding Pause Time
Collector Threads and GC Roots
Tuning Parallel GC
Tuning CMS
Concurrent Mode Failures Due to Fragmentation
Tuning G1
jHiccup
Summary
9. Code Execution on the JVM
Overview of Bytecode Interpretation
Introduction to JVM Bytecode
Simple Interpreters
HotSpot-Specific Details
AOT and JIT Compilation
AOT Compilation
JIT Compilation
Comparing AOT and JIT Compilation
HotSpot JIT Basics
Klass Words, Vtables, and Pointer Swizzling
Logging JIT Compilation
Compilers Within HotSpot
Tiered Compilation in HotSpot
The Code Cache
Fragmentation
Simple JIT Tuning
Summary
10. Understanding JIT Compilation
Introducing JITWatch
Basic JITWatch Views
Debug JVMs and hsdis
Introducing JIT Compilation
Inlining
Inlining Limits
Tuning the Inlining Subsystem
Loop Unrolling
Loop Unrolling Summary
Escape Analysis
Eliminating Heap Allocations
Locks and Escape Analysis
Limitations of Escape Analysis
Monomorphic Dispatch
Intrinsics
On-Stack Replacement
Safepoints Revisited
Core Library Methods
Upper Method Size Limit for Inlining
Upper Method Size Limit for Compilation
Summary
11. Java Language Performance Techniques
Optimizing Collections
Optimization Considerations for Lists
ArrayList
LinkedList
ArrayList versus LinkedList
Optimization Considerations for Maps
HashMap
TreeMap
Lack of MultiMap
Optimization Considerations for Sets
Domain Objects
Avoid Finalization
War Story: Forgetting to Clean Up
Why Not Use Finalization to Solve the Problem?
try-with-resources
Method Handles
Summary
12. Concurrent Performance Techniques
Introduction to Parallelism
Fundamental Java Concurrency
Understanding the JMM
Building Concurrency Libraries
Unsafe
Atomics and CAS
Locks and Spinlocks
Summary of Concurrent Libraries
Locks in java.util.concurrent
Read/Write Locks
Semaphores
Concurrent Collections
Latches and Barriers
Executors and Task Abstraction
Introducing Asynchronous Execution
Selecting an ExecutorService
Fork/Join
Modern Java Concurrency
Streams and Parallel Streams
Lock-Free Techniques
Actor-Based Techniques
Summary
13. Profiling
Introduction to Profiling
Sampling and Safepointing Bias
Execution Profiling Tools for Developers
VisualVM Profiler
JProfiler
YourKit
Flight Recorder and Mission Control
Operational Tools
Modern Profilers
Allocation Profiling
Heap Dump Analysis
hprof
Summary
14. High-Performance Logging and Messaging
Logging
Logging Microbenchmarks
Designing a Lower-Impact Logger
Low Latency Using Real Logic Libraries
Agrona
Simple Binary Encoding
Aeron
The Design of Aeron
Summary
15. Java 9 and the Future
Small Performance Enhancements in Java 9
Segmented Code Cache
Compact Strings
New String Concatenation
C2 Compiler Improvements
New Version of G1
Java 10 and Future Versions
New Release Process
Java 10
Unsafe in Java 9 and Beyond
VarHandles in Java 9
Project Valhalla and Value Types
Graal and Truffle
Future Directions in Bytecode
Future Directions in Concurrency
Conclusion
Index