漫谈Java垃圾收集器

如果无法正常显示,请先停止浏览器的去广告插件。
分享至:
1. 漫谈Java垃圾收集器 公与
2. 什么是垃圾收集 C++ Java
3. Automatic Memory Management • Why we need AMM? • Uniform reference semantics • Multiple references to objects • Complication of code dues to manual memory management • How does automatic memory management solve the problem? • Any object referenced from a machine register is accessible. • Any object referenced from a global variable is accessible. • Any object referenced from the stack is accessible. • Any object referenced from an accessible object is accessible. • Any object not accessible according to the previous definition can be deleted.
4. Problem of AMM? • But isn't garbage collection slow? • So it isn't slow, but doesn't it have these unacceptable pauses? • So can't we use reference counters?
5. JVM最基础的内存分区 JVM逻辑堆 垃圾收集的核心战场 • 内存的分配 • 内存的释放 • 内存的整理
6. 内存的分配 6
7. 内存的释放 0 2 6 GCRoot
8. 内存的整理
9. 问题? • So it isn't slow, but doesn't it have these unacceptable pauses?
10. 分代收集
11. 新生代收集 Serial, ParallelGC 差异 Serial:单线程,STW ParallelGC:多线程, STW
12. 老年代收集 Serial:单线程,标记-清理-整理,STW ParallelGC:多线程,标记-清理-整理,STW CMS 并发GC时代的开始,CMS
13. 并发收集 并行和并发?(GC领域的约定俗成) 并行GC 指GC时应用线程全部被停止,GC使用至少2个或以上的 核心进行GC处理 并发GC 指GC时应用线程不停止,依然可获得CPU时间片 (并 不代表他们一定会物理上和GC线程同时运行)
14. 并发收集 Concurrent Mark Sweep - CMS [GC [1 CMS-initial-mark: 13991K(20288K)] 14103K(22400K), 0.0023781 secs] ... [CMS-concurrent-mark: 0.267/0.374 secs] [CMS-concurrent-preclean: 0.044/0.064 secs] [GC [1 CMS-remark: 16090K(20288K)] 17242K(22400K), 0.0210460 secs] ... [CMS-concurrent-sweep: 0.291/0.662 secs] [CMS-concurrent-reset: 0.016/0.016 secs] • The first pause is to mark as live the objects directly reachable from the roots (for example, object references from application thread stacks and registers, static objects and so on) and from elsewhere in the heap (for example, the young generation). • The second pause comes at the end of the concurrent tracing phase and finds objects that were missed by the concurrent tracing due to updates by the application threads of references in an object after the CMS collector had finished tracing that object.
15. 三色标记法 可达且完成了 子引用扫描 并发标记结束为 什么还会有灰色? 可达但未完成 了子引用扫描 不可达或未扫 描
16. 并发收集所带来的问题 浮动垃圾 – 在并发标记结束后被解除引用 可达对象漏标 – 在并发标记过程中白色对象变更引用
17. 写屏障 • 写屏障 - write barrier • Java o.f = r; • C++ write_barrier(o,&(o->f),r); o->f = r;
18. 那么为何还需要G1? • Mark-Sweep 与 Mark - Sweep - Compact Mark-Sweep Compact 整理
19. G1GC • Region based concurrent collection
20. G1的写屏障实现 • SATB - Snapshot At The Beginning • 在GC开始时做一次对象图快照 • 在引用删除时触发(CMS在引用建立时触发),将删除的的对象推入 GC待扫描 100ms => 10ms ZGC
21. ZGC • Region based. • 不分代 • LVB – Load Value Barrier
22. ZGC的收集周期 collecting-cycle Root Root b c a d f e a Initial mark b c h a f e f g e h g Root Root d d c b Prepare relocate g Pause relocate b a c d e h f g d h d = d’ Root Root Concurren t mark & pause remark concurrent relocate b a c d e f d b e a g h d = d’ e = e’ h = h’ h
23. ZGC的读屏障 - LVB 64 bits 16 bits 44 bits,16TB Known to be marked finalizable mark0 mark1 remapped Only reachable through a Finalizer Known to not point into the relocation set
24. 现代并发GC的调优原则 低暂停不代表高吞吐量 空间换时间与效率 针对G1 & ZGC 加大堆内存(更 多的空余空间)的配置往往更有利 于GC达到目标暂停时间 并发GC是保证并发阶段GC的同时 业务线程依然有几率获得CPU时 间片,但同时也意味着GC会与业 务线程抢占计算资源,且往往更多 的并发阶段为了处理更多的同步问 题,也会占用更多的计算资源 GC调优永远要考虑机器资源,对应 系统应用场景等等,至少目前没有银 弹
25. 关注新生代晋升的情况 -XX:+PrintTenuringDistribution 详细打印ygc时晋升的年龄和大小 -XX:MaxTenuringThreshold=n 设置对象在新生代存活的最大年龄
26. 关注System.gc() -XX:DisableExplicitGC 禁止主动的System.gc -XX:+ExplicitGCInvokesConcurrent 并发执行主动的System.gc
27. 关注DirectBuffer与native area NMT native memory tracing -XX:NativeMemoryTracking=summary or -XX:NativeMemoryTracking=summary
28. 参考文献 • The Z Garbage Collector (java.net) • Pauseless GC (usenix.org) • https://docs.oracle.com/en/java/javase/14/gctuning/z-garbage-collector1.html • https://docs.oracle.com/javase/9/gctuning/garbage-first-garbage- collector.htm • Why we need automatic memory management (labri.fr)
29. THANKS • yin.tong@perfma.com • https://heapdump.cn/

Accueil - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-16 20:47
浙ICP备14020137号-1 $Carte des visiteurs$