1.Introduction 1
A Brief Outline 2
Platforms and Conventions 2
JVM Tuning Flags 4
The Complete Performance Story 5
Write Better Algorithms 5
Write Less Code 6
Oh Go Ahead,Prematurely Optimize 7
Look Elsewhere:The Database Is Always the Bottleneck 8
Optimize for the Common Case 9
Summary 10
2.An Approach to Performance Testing 11
Test a Real Application 11
Microbenchmarks 11
Macrobenchmarks 16
Mesobenchmarks 18
Common Code Examples 20
Understand Throughput,Batching,and Response Time 24
Elapsed Time(Batch) Measurements 24
Throughput Measurements 25
Response Time Tests 26
Understand Variability 29
Test Early,Test Often 33
Summary 36
3.A Java Performance Toolbox 37
Operating System Tools and Analysis 37
CPU Usage 38
The CPU Run Queue 41
Disk Usage 43
Network Usage 44
Java Monitoring Tools 46
Basic VM Information 47
Thread Information 50
Class Information 51
Live GC Analysis 51
Heap Dump Postprocessing 51
Profiling Tools 51
Sampling Profilers 52
Instrumented Profilers 54
Blocking Methods and Thread Timelines 55
Native Profilers 57
Java Mission Control 59
Java Flight Recorder 60
Enabling JFR 66
Selecting JFR Events 70
Summary 72
4.Working with the JIT Compiler 73
Just-in-Time Compilers:An Overview 73
Hot Spot Compilation 75
Basic Tunings:Client or Server(or Both) 77
Optimizing Startup 78
Optimizing Batch Operations 80
Optimizing Long-Running Applications 81
Java and JIT Compiler Versions 81
Intermediate Tunings for the Compiler 85
Tuning the Code Cache 85
Compilation Thresholds 87
Inspecting the Compilation Process 90
Advanced Compiler Tunings 94
Compilation Threads 94
Inlining 96
Escape Analysis 97
Deoptimization 98
Not Entrant Code 98
Deoptimizing Zombie Code 101
Tiered Compilation Levels 101
Summary 103
5.An Introduction to Garbage Collection 105
Garbage Collection Overview 105
Generational Garbage Collectors 107
GC Algorithms 109
Choosing a GC Algorithm 113
Basic GC Tuning 119
Sizing the Heap 119
Sizing the Generations 122
Sizing Permgen and Metaspace 124
Controlling Parallelism 126
Adaptive Sizing 127
GC Tools 128
Summary 131
6.Garbage Collection Algorithms 133
Understanding the Throughput Collector 133
Adaptive and Static Heap Size Tuning 136
Understanding the CMS Collector 140
Tuning to Solve Concurrent Mode Failures 145
Tuning CMS for Permgen 148
Incremental CMS 149
Understanding the G1 Collector 150
Tuning G1 157
Advanced Tunings 159
Tenuring and Survivor Spaces 159
Allocating Large Objects 163
AggressiveHeap 171
Full Control Over Heap Size 173
Summary 174
7.Heap Memory Best Practices 177
Heap Analysis 177
Heap Histograms 178
Heap Dumps 179
Out of Memory Errors 184
Using Less Memory 188
Reducing Object Size 188
Lazy Initialization 191
Immutable and Canonical Objects 196
String Interning 198
Object Lifecycle Management 202
Object Reuse 202
Weak,Soft,and Other References 208
Summary 221
8.Native Memory Best Practices 223
Footprint 223
Measuring Footprint 224
Minimizing Footprint 225
Native NIO Buffers 226
Native Memory Tracking 227
JVM Tunings for the Operating System 230
Large Pages 230
Compressed oops 234
Summary 236
9.Threading and Synchronization Performance 237
Thread Pools and ThreadPoolExecutors 237
Setting the Maximum Number of Threads 238
Setting the Minimum Number of Threads 242
Thread Pool Task Sizes 243
Sizing a ThreadPoolExecutor 244
The ForkJoinPool 246
Automatic Parallelization 252
Thread Synchronization 254
Costs of Synchronization 254
Avoiding Synchronization 259
False Sharing 262
JVM Thread Tunings 267
Tuning Thread Stack Sizes 267
Biased Locking 268
Lock Spinning 268
Thread Priorities 269
Monitoring Threads and Locks 270
Thread Visibility 270
Blocked Thread Visibility 271
Summary 275
10.Java Enterprise Edition Performance 277
Basic Web Container Performance 277
HTTP Session State 280
Thread Pools 283
Enterprise Java Session Beans 283
Tuning EJB Pools 283
Tuning EJB Caches 286
Local and Remote Instances 288
XML and JSON Processing 289
Data Size 290
An Overview of Parsing and Marshalling 291
Choosing a Parser 293
XML Validation 299
Document Models 302
Java Object Models 305
Object Serialization 307
Transient Fields 307
Overriding Default Serialization 307
Compressing Serialized Data 311
Keeping Track of Duplicate Objects 313
Java EE Networking APIs 316
Sizing Data Transfers 316
Summary 319
11.Database Performance Best Practices 321
JDBC 322
JDBC Drivers 322
Prepared Statements and Statement Pooling 324
JDBC Connection Pools 326
Transactions 327
Result Set Processing 335
JPA 337
Transaction Handling 337
Optimizing JPA Writes 340
Optimizing JPA Reads 342
JPA Caching 346
JPA Read-Only Entities 352
Summary 353
12.Java SE API Tips 355
Buffered I/O 355
Classloading 358
Random Numbers 362
Java Native Interface 364
Exceptions 366
String Performance 370
Logging 371
Java Collections API 373
Synchronized Versus Unsynchronized 373
Collection Sizing 375
Collections and Memory Efficiency 376
AggressiveOpts 378
Alternate Implementations 378
Miscellaneous Flags 379
Lambdas and Anonymous Classes 379
Lambda and Anonymous Classloading 381
Stream and Filter Performance 382
Lazy Traversal 383
Summary 385
A.Summary of Tuning Flags 387
Index 397