TL Java性能权威指南
第 1 章 导论 .........................................................................................................................................1
1.1 ߁.............................................................................................................................................ຎ2
1.2 ೝ台版本ሀ定.............................................................................................................................2
1.3 全面的性能调优.........................................................................................................................4
1.3.1 编写更好的算法 ............................................................................................................4
1.3.2 编写更少的代码 ............................................................................................................4
1.3.3 老调重弹的过早优化 ....................................................................................................5
1.3.4 其他:数据库很可能就是瓶颈 ....................................................................................6
1.3.5 常见的优化 ....................................................................................................................7
1.4 ၭ.............................................................................................................................................8
第 2 章 性能测试方法 .......................................................................................................................9
2.1 原ሶ 1:֪ኈ实应用..............................................................................................................9
2.1.1 微基准测试 ....................................................................................................................9
2.1.2 宏基准测试 ..................................................................................................................13
2.1.3 介基准测试 ..................................................................................................................15
2.1.4 代码示例 ......................................................................................................................16
2.2 原ሶ 2:解಼تୁ时क़、吞吐量和响应时क़............................................................19
2.2.1 批处理流逝时间 ..........................................................................................................19
2.2.2 吞吐量测试 ..................................................................................................................20
2.2.3 响应时间测试 ..............................................................................................................20
2.3 原ሶ 3:用统ऺݛ݆应对性能的Վ化....................................................................................23
2.4 原ሶ 4:ራೕݏ............................................................................................................֪26
vi | 目录
2.5 ၭ ...........................................................................................................................................28
第 3 章 Java 性能调优工具箱 ......................................................................................................29
3.1 ֡作ဣ统的工具和分析...........................................................................................................29
3.1.1 CPU 使用率..................................................................................................................29
3.1.2 CPU 运行队列..............................................................................................................32
3.1.3 磁盘使用率 ..................................................................................................................33
3.1.4 网络使用率 ..................................................................................................................34
3.2 Java ॔੦工具...........................................................................................................................35
3.2.1 基本的 VM 信息..........................................................................................................36
3.2.2 线程信息 ......................................................................................................................39
3.2.3 类信息 ..........................................................................................................................39
3.2.4 实时 GC 分析...............................................................................................................39
3.2.5 事后堆转储 ..................................................................................................................39
3.3 性能分析工具...........................................................................................................................39
3.3.1 采样分析器 ..................................................................................................................40
3.3.2 探查分析器 ..................................................................................................................41
3.3.3 阻塞方法和线程时间线 ..............................................................................................42
3.3.4 本地分析器 ..................................................................................................................44
3.4 Java 任ခ੦制...........................................................................................................................45
3.4.1 Java 飞行记录器 ..........................................................................................................46
3.4.2 开启 JFR.......................................................................................................................52
3.4.3 选择 JFR 事件..............................................................................................................54
3.5 ၭ...........................................................................................................................................56
第 4 章 JIT 编译器 ...........................................................................................................................58
4.1 JIT 编译器:߁બ.....................................................................................................................58
4.2 调优入ோ:ስ编译器ૌ႙DŽClient、Server ईܾኁ同用Dž................................................61
4.2.1 优化启动 ......................................................................................................................62
4.2.2 优化批处理 ..................................................................................................................63
4.2.3 优化长时间运行的应用 ..............................................................................................64
4.3 Java 和 JIT 编译器版本 ...........................................................................................................64
4.4 编译器中प调优.......................................................................................................................67
4.4.1 调优代码缓存 ..............................................................................................................67
4.4.2 编译阈值 ......................................................................................................................68
4.4.3 检测编译过程 ..............................................................................................................70
4.5 ߛप编译器调优.......................................................................................................................73
4.5.1 编译线程 ......................................................................................................................73
4.5.2 内联 ..............................................................................................................................74
目录 | vii
4.5.3 逃逸分析 ......................................................................................................................75
4.6 ొ优化.......................................................................................................................................76
4.6.1 代码被丢弃 ..................................................................................................................77
4.6.2 逆优化僵尸代码 ..........................................................................................................78
4.7 分֫编译प՚...........................................................................................................................79
4.8 ၭ...........................................................................................................................................80
第 5 章 垃圾收集入门 .....................................................................................................................81
5.1 垃圾收集߁...........................................................................................................................ຎ81
5.1.1 分代垃圾收集器 ..........................................................................................................83
5.1.2 GC 算法........................................................................................................................84
5.1.3 选择 GC 算法...............................................................................................................87
5.2 GC 调优एإ............................................................................................................................ 92
5.2.1 调整堆的大小 ..............................................................................................................92
5.2.2 代空间的调整 ..............................................................................................................95
5.2.3 永久代和元空间的调整 ..............................................................................................96
5.2.4 控制并发 ......................................................................................................................97
5.2.5 自适应调整 ..................................................................................................................98
5.3 垃圾࣮收工具...........................................................................................................................99
5.4 ၭ.........................................................................................................................................102
第 6 章 垃圾收集算法 ...................................................................................................................103
6.1 解 Throughput 收集器........................................................................................................103
6.2 解 CMS 收集器 ..................................................................................................................109
6.2.1 针对并发模式失效的调优 ........................................................................................113
6.2.2 CMS 收集器的永久代调优.......................................................................................116
6.2.3 增量式 CMS 垃圾收集..............................................................................................117
6.3 解 G1 垃圾收集器..............................................................................................................118
6.4 ߛप调优.................................................................................................................................126
6.4.1 晋升及 Survivor 空间 ................................................................................................126
6.4.2 分配大对象 ................................................................................................................129
6.4.3 AggressiveHeap 标志 ................................................................................................136
6.4.4 全盘掌控堆空间的大小 ............................................................................................137
6.5 ၭ.........................................................................................................................................138
第 7 章 堆内存最佳实践 ..............................................................................................................140
7.1 堆分析.....................................................................................................................................140
7.1.1 堆直方图 ....................................................................................................................141
7.1.2 堆转储 ........................................................................................................................142
7.1.3 内存溢出错误 ............................................................................................................146
viii | 目录
7.2 ३ณ内存使用 .........................................................................................................................149
7.2.1 减少对象大小 ............................................................................................................149
7.2.2 延迟初始化 ................................................................................................................152
7.2.3 不可变对象和标准化对象 ........................................................................................156
7.2.4 字符串的保留 ............................................................................................................157
7.3 对ၡ生ంዜ࠶.................................................................................................................160
7.3.1 对象重用 ....................................................................................................................160
7.3.2 弱引用、软引用与其他引用 ....................................................................................165
7.4 ၭ.........................................................................................................................................175
第 8 章 原生内存最佳实践 ..........................................................................................................176
8.1 内存用.................................................................................................................................176
8.1.1 测量内存占用 ............................................................................................................177
8.1.2 内存占用最小化 ........................................................................................................178
8.1.3 原生 NIO 缓冲区 .......................................................................................................178
8.1.4 原生内存跟踪 ............................................................................................................179
8.2 ኍ对不同֡作ဣ统优化 JVM................................................................................................182
8.2.1 大页 ............................................................................................................................182
8.2.2 压缩的 oop .................................................................................................................185
8.3 ၭ.........................................................................................................................................187
第 9 章 线程与同步的性能 ..........................................................................................................188
9.1 线程与 ThreadPoolExecutor .............................................................................................188
9.1.1 设置最大线程数 ........................................................................................................189
9.1.2 设置最小线程数 ........................................................................................................192
9.1.3 线程池任务大小 ........................................................................................................193
9.1.4 设置 ThreadPoolExecutor 的大小 ............................................................................193
9.2 ForkJoinPool ..........................................................................................................................195
9.3 线程同步.................................................................................................................................201
9.3.1 同步的代价 ................................................................................................................202
9.3.2 避免同步 ....................................................................................................................205
9.3.3 伪共享 ........................................................................................................................208
9.4 JVM 线程调优........................................................................................................................211
9.4.1 调节线程栈大小 ........................................................................................................211
9.4.2 偏向锁 ........................................................................................................................212
9.4.3 自旋锁 ........................................................................................................................212
9.4.4 线程优先级 ................................................................................................................213
9.5 ॔੦线程与.........................................................................................................................213
9.5.1 查看线程 ....................................................................................................................214
目录 | ix
9.5.2 查看阻塞线程 ............................................................................................................214
9.6 ၭ.........................................................................................................................................217
第 10 章 Java EE 性能调优 ........................................................................................................218
10.1 Web 容器的ए本性能 ..........................................................................................................218
10.2 线程...................................................................................................................................222
10.3 EJB ࣆࣷ Bean......................................................................................................................223
10.3.1 调优 EJB 对象池 ....................................................................................................223
10.3.2 调优 EJB 缓存 ........................................................................................................225
10.3.3 本地和远程实例.....................................................................................................226
10.4 XML 和 JSON ت............................................................................................................. 227
10.4.1 数据大小.................................................................................................................227
10.4.2 解析和编组概述.....................................................................................................229
10.4.3 选择解析器.............................................................................................................230
10.4.4 XML 验证...............................................................................................................235
10.4.5 文档模型.................................................................................................................237
10.4.6 Java 对象模型.........................................................................................................240
10.5 对ၡႾଚ化...........................................................................................................................241
10.5.1 transient 字段 .......................................................................................................241
10.5.2 覆盖默认的序列化.................................................................................................241
10.5.3 压缩序列化数据.....................................................................................................244
10.5.4 追踪对象复制.........................................................................................................246
10.6 Java EE 网ஏ API .................................................................................................................248
10.7 ၭ.......................................................................................................................................250
第 11 章 数据库性能的最佳实践 ...............................................................................................251
11.1 JDBC.....................................................................................................................................251
11.1.1 JDBC 驱动程序 ......................................................................................................252
11.1.2 预处理语句和语句池.............................................................................................253
11.1.3 JDBC 连接池 ..........................................................................................................255
11.1.4 事务.........................................................................................................................256
11.1.5 结果集的处理.........................................................................................................262
11.2 JPA ........................................................................................................................................264
11.2.1 事务处理.................................................................................................................264
11.2.2 对 JPA 的写性能进行优化.....................................................................................267
11.2.3 对 JPA 的读性能进行优化.....................................................................................268
11.2.4 JPA 缓存..................................................................................................................271
11.2.5 JPA 的只读实体......................................................................................................276
11.3 ၭ.......................................................................................................................................277
x | 目录
第 12 章 Java SE API 技巧 ........................................................................................................278
12.1 ؋࣐๕ I/O.............................................................................................................................278
12.2 ૌेሜ...................................................................................................................................280
12.3 ໜऐ数...................................................................................................................................284
12.4 Java 原生থ੨.......................................................................................................................285
12.5 ᅴ.......................................................................................................................................287
12.6 字زޙ的性能.......................................................................................................................290
12.7 නኾ.......................................................................................................................................291
12.8 Java 集合ૌ API ...................................................................................................................292
12.8.1 同步还是非同步.....................................................................................................293
12.8.2 设定集合的大小.....................................................................................................294
12.8.3 集合与内存使用效率.............................................................................................295
12.9 AggressiveOpts Քኾ ...........................................................................................................296
12.9.1 替代实现.................................................................................................................296
12.9.2 其他标志.................................................................................................................297
12.10 Lambda ٳ๕和ైఁૌ...................................................................................................297
12.11 ୁ和ࡗ୳器的性能.............................................................................................................300
12.12 ၭ.....................................................................................................................................302
附录 A 性能调优标志摘要 ...........................................................................................................303
作者简介 ..............................................................................................................................................312
关于封面 ..............................................................................................................................................312
**** Hidden Message *****
啥也不说了,感谢楼主分享哇! 啥也不说了,感谢楼主分享哇! 啥也不说了,感谢楼主分享哇! 确实是难得好帖啊,顶先 啥也不说了,感谢楼主分享哇! 啥也不说了,感谢楼主分享哇! test
啥也不说了,感谢楼主分享哇! 啥也不说了,感谢楼主分享哇! 啥也不说了,感谢楼主分享哇!
页:
[1]
2