Sun jdk 1.6内存管理 -实现篇 -毕玄

60
Sun JDK 1.6 内存管理 实现篇 毕玄 2010-102010-11

Transcript of Sun jdk 1.6内存管理 -实现篇 -毕玄

Page 1: Sun jdk 1.6内存管理 -实现篇 -毕玄

Sun JDK 1.6 内存管理实现篇

毕玄

2010-10、2010-11

Page 2: Sun jdk 1.6内存管理 -实现篇 -毕玄

目标

• 掌握内存管理的通常实现方法

• 了解Sun JDK是如何实现内存管理的,以及做了哪些优化

• 了解JRockit做了哪些优化

Page 3: Sun jdk 1.6内存管理 -实现篇 -毕玄

OS内存管理

图片来源于JavaOne 2010 《where does all native memory go》 Session

Page 4: Sun jdk 1.6内存管理 -实现篇 -毕玄

OS内存管理

图片来源于JavaOne 2010 《where does all native memory go》 Session

Page 5: Sun jdk 1.6内存管理 -实现篇 -毕玄

OS内存管理

图片来源于JavaOne 2010 《where does all native memory go》 Session

Page 6: Sun jdk 1.6内存管理 -实现篇 -毕玄

OS内存管理

图片来源于JavaOne 2010 《where does all native memory go》 Session

Page 7: Sun jdk 1.6内存管理 -实现篇 -毕玄

OS内存管理

图片来源于JavaOne 2010 《where does all native memory go》 Session

Page 8: Sun jdk 1.6内存管理 -实现篇 -毕玄

内存分配和回收

• allocate

A B C D

Page 9: Sun jdk 1.6内存管理 -实现篇 -毕玄

内存分配和回收

• allocate

A B C B

freelist

Page 10: Sun jdk 1.6内存管理 -实现篇 -毕玄

内存分配和回收

• fragmenation

A B C B

freelist

E

i need allocate

sorry,no space

Page 11: Sun jdk 1.6内存管理 -实现篇 -毕玄

内存分配和回收

• Compaction

– need update pointer

A BC B

free space pointer

Page 12: Sun jdk 1.6内存管理 -实现篇 -毕玄

Discussion

• how to find garbage and claim it..

– let’s talk about this question first

Page 13: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm

• Reference Counting

• Tracing

– Mark-Sweep

– Mark-Compact

– Copying

Page 14: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 15: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 16: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 17: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 18: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 19: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 20: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 21: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Reference Counting

– extra space,time overhead

– non-moving

– cyclic garbage

Page 22: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Advance Reference Counting

– two-bit reference counts

• when max count(3) is reached,object becomes “sticky”

– buffer reference updates

– use a backup gc algorithm to handle cyclic garbage and “sticky” objects

– complex,but still non-moving

Page 23: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 24: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 25: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 26: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 27: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 28: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 29: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Sweep

– need scan all objects,so if java heap becomes larger,then gc slower

– 可清除所有的Garbage

– 内存碎片,分配低效

– 需要在一定的时机触发

Page 30: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Compact

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 31: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Mark-Compact

– 没有内存碎片,分配高效

– 增加了回收需要耗费的时间

– 需要更新所有移动过的object的ref pointer

– 需要在一定时机触发

Page 32: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 33: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 34: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 35: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 36: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 37: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

图片来源于JavaOne 2010 《The Garbage Collection Mythbusters》 Session by tony,john

Page 38: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Details

• Tracing – Copying(a special case)

– only need scan live objects,so gc speed only decided by lds

– no fragmentation

– need update object pointer

– need keep an empty memory area

Page 39: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC Algorithm Summary

• Why gc need stop the world?

– Mark-Sweep,Mark-Compact

• if ref changes or new object created when marking;

• compact need update pointer,so...

– Copying

• copying need update pointer,so...

Page 40: Sun jdk 1.6内存管理 -实现篇 -毕玄

目标

• 掌握内存管理的通常实现方法

• 了解Sun JDK是如何实现内存管理的,以及做了哪些优化

• 了解JRockit做了哪些优化

Page 41: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• rootset

– runtime stack

– static or global variables

– jni handles

– jvm handles

Page 42: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• Generational GC

– most objects are temp-lived

• so JDK decide split heap into two generations to use gcalgorithm properly

New Old

Copying Mark-Sweep or Mark-Compact

Page 43: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• how to stop the world

– safepoint at ref change

• safepoint is a memory page check

• compile

– when gc need execute,it submit “stop thread” request to jvm core,then jvm core set the memory page not readable

– when code execute to safepoint, check the memory page if readable,if not then fail and stop

Page 44: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• YGC

– allocate is slow,because need lock

– how to make allocate faster?

• do u have any other ideas?

TLAB Use CAS replace lock

Page 45: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• YGC

– how to find live objects faster?

• do u have any other ideas?

Live Maps Scan Algorithm

Page 46: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• YGC

– how to handle object in old gen ref new gen object

• do u have any other ideas?

Write Barrier & Card table

Page 47: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• YGC

– how to make scan and copying faster?

• do u have any other ideas?

ParallelAdaptive based on runtime feedback

Page 48: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC

– optimize for throughput

• parallel

– optimize for low latency

• concurrent

Page 49: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC – parallel

– based on Mark-Compact

– allocate use the bump pointer

Page 50: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC – parallel

– how to make mark phase faster?

• do u have any other ideas?Parallel

Page 51: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC – parallel

– how to make compact phase faster?

• do u have any other ideas?

Partial Compact when use ParallelOldGC

Page 52: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC – concurrent

– based on mark-sweep

– when concurrent gc executes,ref may be changed and new objects may be created,how to solve the question?

Page 53: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC – concurrent

– when concurrent gc executes,ref may be changed and new objects may be created,how to solve the question?

Write Barrier & Card Table

Page 54: Sun jdk 1.6内存管理 -实现篇 -毕玄

GC in Hotspot

• FGC – concurrent

– how to mark remark phase faster?

• do u have any other ideas?

add a preclean phase

Page 55: Sun jdk 1.6内存管理 -实现篇 -毕玄

Futhermore – G1

• Region-based GC

• how to behave better?

garbage first

hot regions

Page 56: Sun jdk 1.6内存管理 -实现篇 -毕玄

目标

• 掌握内存管理的通常实现方法

• 了解Sun JDK是如何实现内存管理的,以及做了哪些优化

• 了解JRockit做了哪些优化

Page 57: Sun jdk 1.6内存管理 -实现篇 -毕玄

Futhermore – Open eyes

• JRockit GC

– YGC

• pinned object for semi-long lived object

• keep area

– Mark

• Two-Color for prefetching

– Compaction

• partial,for example some objects not be compacted because many objects ref them.

– Adjust gens,size adaptive based on runtime feedback

Page 58: Sun jdk 1.6内存管理 -实现篇 -毕玄

目标

• 掌握内存管理的通常实现方法

• 了解Sun JDK是如何实现内存管理的,以及做了哪些优化

• 了解JRockit做了哪些优化

Page 59: Sun jdk 1.6内存管理 -实现篇 -毕玄

how to research jvm

• JDK Source codes

• References

Page 60: Sun jdk 1.6内存管理 -实现篇 -毕玄

References

• JVM References