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
Author(s): Scott Oaks
Edition: 2
Language: English
Commentary: True PDF
Pages: 450
Tags: Databases; Multithreading; Java; Monitoring; Memory Management; Profiling; Best Practices; Performance Testing; Garbage Collection; Performance
Cover
Copyright
Table of Contents
Preface
Who Should (and Shouldn’t) Read This Book
What’s New in the Second Edition
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Introduction
A Brief Outline
Platforms and Conventions
Java Platforms
Hardware Platforms
The Complete Performance Story
Write Better Algorithms
Write Less Code
Oh, Go Ahead, Prematurely Optimize
Look Elsewhere: The Database Is Always the Bottleneck
Optimize for the Common Case
Summary
Chapter 2. An Approach to Performance Testing
Test a Real Application
Microbenchmarks
Macrobenchmarks
Mesobenchmarks
Understand Throughput, Batching, and Response Time
Elapsed Time (Batch) Measurements
Throughput Measurements
Response-Time Tests
Understand Variability
Test Early, Test Often
Benchmark Examples
Java Microbenchmark Harness
Common Code Examples
Summary
Chapter 3. A Java Performance Toolbox
Operating System Tools and Analysis
CPU Usage
The CPU Run Queue
Disk Usage
Network Usage
Java Monitoring Tools
Basic VM Information
Thread Information
Class Information
Live GC Analysis
Heap Dump Postprocessing
Profiling Tools
Sampling Profilers
Instrumented Profilers
Blocking Methods and Thread Timelines
Native Profilers
Java Flight Recorder
Java Mission Control
JFR Overview
Enabling JFR
Selecting JFR Events
Summary
Chapter 4. Working with the JIT Compiler
Just-in-Time Compilers: An Overview
HotSpot Compilation
Tiered Compilation
Common Compiler Flags
Tuning the Code Cache
Inspecting the Compilation Process
Tiered Compilation Levels
Deoptimization
Advanced Compiler Flags
Compilation Thresholds
Compilation Threads
Inlining
Escape Analysis
CPU-Specific Code
Tiered Compilation Trade-offs
The GraalVM
Precompilation
Ahead-of-Time Compilation
GraalVM Native Compilation
Summary
Chapter 5. An Introduction to Garbage Collection
Garbage Collection Overview
Generational Garbage Collectors
GC Algorithms
Choosing a GC Algorithm
Basic GC Tuning
Sizing the Heap
Sizing the Generations
Sizing Metaspace
Controlling Parallelism
GC Tools
Enabling GC Logging in JDK 8
Enabling GC Logging in JDK 11
Summary
Chapter 6. Garbage Collection Algorithms
Understanding the Throughput Collector
Adaptive and Static Heap Size Tuning
Understanding the G1 Garbage Collector
Tuning G1 GC
Understanding the CMS Collector
Tuning to Solve Concurrent Mode Failures
Advanced Tunings
Tenuring and Survivor Spaces
Allocating Large Objects
AggressiveHeap
Full Control Over Heap Size
Experimental GC Algorithms
Concurrent Compaction: ZGC and Shenandoah
No Collection: Epsilon GC
Summary
Chapter 7. Heap Memory Best Practices
Heap Analysis
Heap Histograms
Heap Dumps
Out-of-Memory Errors
Using Less Memory
Reducing Object Size
Using Lazy Initialization
Using Immutable and Canonical Objects
Object Life-Cycle Management
Object Reuse
Soft, Weak, and Other References
Compressed Oops
Summary
Chapter 8. Native Memory Best Practices
Footprint
Measuring Footprint
Minimizing Footprint
Native Memory Tracking
Shared Library Native Memory
JVM Tunings for the Operating System
Large Pages
Summary
Chapter 9. Threading and Synchronization Performance
Threading and Hardware
Thread Pools and ThreadPoolExecutors
Setting the Maximum Number of Threads
Setting the Minimum Number of Threads
Thread Pool Task Sizes
Sizing a ThreadPoolExecutor
The ForkJoinPool
Work Stealing
Automatic Parallelization
Thread Synchronization
Costs of Synchronization
Avoiding Synchronization
False Sharing
JVM Thread Tunings
Tuning Thread Stack Sizes
Biased Locking
Thread Priorities
Monitoring Threads and Locks
Thread Visibility
Blocked Thread Visibility
Summary
Chapter 10. Java Servers
Java NIO Overview
Server Containers
Tuning Server Thread Pools
Async Rest Servers
Asynchronous Outbound Calls
Asynchronous HTTP
JSON Processing
An Overview of Parsing and Marshaling
JSON Objects
JSON Parsing
Summary
Chapter 11. Database Performance Best Practices
Sample Database
JDBC
JDBC Drivers
JDBC Connection Pools
Prepared Statements and Statement Pooling
Transactions
Result Set Processing
JPA
Optimizing JPA Writes
Optimizing JPA Reads
JPA Caching
Spring Data
Summary
Chapter 12. Java SE API Tips
Strings
Compact Strings
Duplicate Strings and String Interning
String Concatenation
Buffered I/O
Classloading
Class Data Sharing
Random Numbers
Java Native Interface
Exceptions
Logging
Java Collections API
Synchronized Versus Unsynchronized
Collection Sizing
Collections and Memory Efficiency
Lambdas and Anonymous Classes
Stream and Filter Performance
Lazy Traversal
Object Serialization
Transient Fields
Overriding Default Serialization
Compressing Serialized Data
Keeping Track of Duplicate Objects
Summary
Appendix A. Summary of Tuning Flags
Index
About the Author
Colophon