O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Optimizing Java

Book Description

Performance tuning is an experimental science, but that doesn’t mean engineers should resort to guesswork and folklore to get the job done. Yet that’s often the case. With this practical book, intermediate to advanced Java technologists working with complex technology stacks will learn how to tune Java applications for performance using a quantitative, verifiable approach.

Most resources on performance tend to discuss the theory and internals of Java virtual machines, but this book focuses on the practicalities of performance tuning by examining a wide range of aspects. There are no simple recipes, tips and tricks, or algorithms to learn. Performance tuning is a process of defining and determining desired outcomes. And it requires diligence.

  • Learn how Java principles and technology make the best use of modern hardware and operating systems
  • Explore several performance tests and common anti-patterns that can vex your team
  • Understand the pitfalls of measuring Java performance numbers and the drawbacks of microbenchmarking
  • Dive into JVM garbage collection logging, monitoring, tuning, and tools
  • Explore JIT compilation and Java language performance techniques
  • Learn performance aspects of the Java Collections API and get an overview of Java concurrency

Table of Contents

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