Data Caching, Garbage Collection, and the Java Memory Model
Wolfgang Puffitsch wpuffits@mail.tuwien.ac.at JTRES ’09, September 23-25, 2009
1 / 24
Data Caching, Garbage Collection, and the Java Memory Model - - PowerPoint PPT Presentation
Data Caching, Garbage Collection, and the Java Memory Model Wolfgang Puffitsch wpuffits@mail.tuwien.ac.at JTRES 09, September 23-25, 2009 1 / 24 Motivation I Sequential consistency is expensive Multi-processors often implement
1 / 24
◮ Sequential consistency is expensive ◮ Multi-processors often implement
◮ JMM is a logical choice for a Java
2 / 24
◮ JMM specifies memory model for
◮ JMM is agnostic of run-time system ◮ Minimal communication between
◮ Asymmetric synchronization 3 / 24
◮ Happens-before relation ◮ Similar to lazy release consistency ◮ Allows various optimizations ◮ Rules out a number of odd behaviors
◮ Causality must be obeyed 4 / 24
5 / 24
◮ Implemented for JopCMP ◮ Predictable, low HW cost ◮ Follows idea of lazy release consistency ◮ Invalidate cache on monitorenter and
◮ Write-through cache
6 / 24
◮ No global store order ◮ Accesses cannot bypass each other
◮ Relatively simple memory model
◮ Good predictability
◮ Consistency actions are always local 7 / 24
◮ Only minimal communication between
◮ Avoid synchronization overhead for
◮ How to force application to see moved
◮ Invalidate cache for each moved object ◮ Stronger memory model ◮ Avoid movement of objects 8 / 24
void runGC () { // i n i t i a t e new GC c y c l e s t a r t C y c l e ( ) ; // r e t r i e v e root s gatherRoots ( ) ; // t r a c e the
graph traceObjectGraph ( ) ; // c l e a r
that are s t i l l white sweepUnusedObjects ( ) ; //
defragment ( ) ; }
9 / 24
◮ White objects have not been visited ◮ Gray objects need to be visited ◮ Black objects have been visited ◮ After tracing, reachable objects are
10 / 24
void traceObjectGraph () { // while there are s t i l l gray
while ( ! grayObjects . isEmpty ( ) ) { // get a gray
Object
// i t e r a t e
a l l r e f e r e n c e f i e l d s for ( F i e l d f in g e t R e f F i e l d s ( obj )) { Object f i e l d V a l = g e t F i e l d ( obj , f ) ; // mark r e f e r e n c e d
i f ( c o l o r ( f i e l d V a l ) == white ) { markGray ( f i e l d V a l ) ; } } markBlack ( obj ) ; }}
11 / 24
void putFieldRef ( Object
F i e l d f , Object newVal ) { // snapshot−at−beginning b a r r i e r Object
f ) ; i f ( c o l o r ( oldVal ) == white ) { markGray ( oldVal ) ; } // w r i t e new value to f i e l d p u t F i e l d ( obj , f , newVal ) ; }
12 / 24
◮ a snapshot-at-beginning write barrier is
◮ new objects are allocated non-white, and ◮ a consensus is established at the
13 / 24
◮ Objects must either be reachable from
◮ Differences in object graph views must
◮ Concurrent updates must see snapshot
◮ Works for our cache implementation ◮ Not guaranteed in JMM! 14 / 24
15 / 24
◮ Consensus is established by invalidating
◮ How to make this non-atomically?
◮ Sliding view root scanning ◮ Invalidate cache at root scanning
◮ Assuming double-barrier
◮ Both old and new value are shaded 16 / 24
◮ Field updates from earlier GC cycles
◮ Field updates from earlier GC cycles
◮ Field updates from earlier GC cycles
17 / 24
◮ Clear separation of GC cycles ◮ Threads that are preempted while
18 / 24
◮ Costs of implementation choices to be
◮ Avoid overlap of old and new barriers
◮ Handshake or mutual exclusion
◮ Enforce consistent perception in
◮ Bypass cache or cache invalidation 19 / 24
◮ Threads must see default values ◮ Avoid synchronization between
◮ Memory must not have been in use
◮ Cache invalidation at GC cycle start ⇒
◮ Analogue consideration for final values
20 / 24
◮ Inter-thread communication of GC
◮ Internal data structures can follow own
◮ E.g., bypass cache ◮ Avoids merging application and run-time
◮ Depends on capabilities of platform 21 / 24
◮ Cache that is consistent with JMM ◮ Moving of objects needs consistency
◮ Tracing works if JMM surprising
◮ Start of GC cycle requires careful design
22 / 24
◮ Object creation simple in some cases ◮ Run-time system synchronization can be
23 / 24
24 / 24