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

Post on 20-Aug-2015

989 views 3 download

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

Sun JDK 1.6 内存管理实现篇

毕玄

2010-10、2010-11

目标

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

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

• 了解JRockit做了哪些优化

OS内存管理

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

OS内存管理

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

OS内存管理

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

OS内存管理

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

OS内存管理

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

内存分配和回收

• allocate

A B C D

内存分配和回收

• allocate

A B C B

freelist

内存分配和回收

• fragmenation

A B C B

freelist

E

i need allocate

sorry,no space

内存分配和回收

• Compaction

– need update pointer

A BC B

free space pointer

Discussion

• how to find garbage and claim it..

– let’s talk about this question first

GC Algorithm

• Reference Counting

• Tracing

– Mark-Sweep

– Mark-Compact

– Copying

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

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

GC Algorithm Details

• Reference Counting

– extra space,time overhead

– non-moving

– cyclic garbage

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

GC Algorithm Details

• Tracing – Mark-Sweep

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

– 可清除所有的Garbage

– 内存碎片,分配低效

– 需要在一定的时机触发

GC Algorithm Details

• Tracing – Mark-Compact

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

GC Algorithm Details

• Tracing – Mark-Compact

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

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

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

– 需要在一定时机触发

GC Algorithm Details

• Tracing – Copying(a special case)

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

GC Algorithm Details

• Tracing – Copying(a special case)

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

GC Algorithm Details

• Tracing – Copying(a special case)

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

GC Algorithm Details

• Tracing – Copying(a special case)

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

GC Algorithm Details

• Tracing – Copying(a special case)

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

GC Algorithm Details

• Tracing – Copying(a special case)

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

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

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...

目标

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

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

• 了解JRockit做了哪些优化

GC in Hotspot

• rootset

– runtime stack

– static or global variables

– jni handles

– jvm handles

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

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

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

GC in Hotspot

• YGC

– how to find live objects faster?

• do u have any other ideas?

Live Maps Scan Algorithm

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

GC in Hotspot

• YGC

– how to make scan and copying faster?

• do u have any other ideas?

ParallelAdaptive based on runtime feedback

GC in Hotspot

• FGC

– optimize for throughput

• parallel

– optimize for low latency

• concurrent

GC in Hotspot

• FGC – parallel

– based on Mark-Compact

– allocate use the bump pointer

GC in Hotspot

• FGC – parallel

– how to make mark phase faster?

• do u have any other ideas?Parallel

GC in Hotspot

• FGC – parallel

– how to make compact phase faster?

• do u have any other ideas?

Partial Compact when use ParallelOldGC

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?

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

GC in Hotspot

• FGC – concurrent

– how to mark remark phase faster?

• do u have any other ideas?

add a preclean phase

Futhermore – G1

• Region-based GC

• how to behave better?

garbage first

hot regions

目标

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

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

• 了解JRockit做了哪些优化

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

目标

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

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

• 了解JRockit做了哪些优化

how to research jvm

• JDK Source codes

• References

References

• JVM References