@jamie allen -...

19

Transcript of @jamie allen -...

Page 1: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model
Page 2: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

@jamie_allen

Mutability Matrix of Pain 2

Page 3: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Agenda

• The Java Memory Model

• What do concurrent and parallel mean?

• How does the heap work?

• How do we protect shared state on the heap?

Mutability Matrix of Pain 3

Page 4: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Scaling in the Multicore Era

• We have more more cores than ever (Intel Xeon E5-2699-v3 has 18 cores!)

• Shared mutable state, and the contention between threads of execution vying to update them simultaneously, is a performance killer• Blocking• Locking

Mutability Matrix of Pain 4

Page 5: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

The JVM is awesome!

• The power to leverage all cores on a box through a single, unified process

• The first formal memory model, with well-defined semantics for visibility

Mutability Matrix of Pain 5

Diagram by Juan Pablo Francisconihttp://www.researchbeta.com/?p=334

Page 6: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

The Java Memory Model

• Two primary concepts:• Happens-Before – if A happens before B, the A's result should be

visible to B• Synchronize-With – action results in write-through to RAM before next

action

Mutability Matrix of Pain 6

Page 7: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

The Java Memory Model

• There are no visibility guarantees between threads in a JVM without locks or memory barriers (volatile)

• Something may end up being visible to another thread by pure happenstance

• Contended locks are arbitrated at the kernel level, leading to expensive context switches

Mutability Matrix of Pain 7

Page 8: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Concurrent and Parallel

Mutability Matrix of Pain 8

Page 9: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Name Versus Value

Mutability Matrix of Pain 9

• The name is the handle you (or the compiler) bind to a reference

• The value is the actual state stored in the heap

• Therefore, the name is a reference to a location on the heap

Name Value

val me = new Person("Jamie", "Allen");

Page 10: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Name Versus Value

Mutability Matrix of Pain 10

• For instance variables, a heap location for the class instance holds the reference via the name field

• The value itself is located elsewhere (possibly contiguous, no guarantee)class Customer {

val me = new Person("Jamie", "Allen");

...

}

Person("Jamie", "Allen")

me

Page 11: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Collections

Mutability Matrix of Pain 11

• Collections are an aggregation of references to values

• Java collections are all mutable by default, developers have to use Guava to get immutable collectionsval me = new Person("Jamie", "Allen");

val other = new Person("Yeon", "Kim");

val us = List(me, other);

Person("Jamie", "Allen")

Person("Yeon", "Kim")

us

me

other

Page 12: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Method-Scoped Name Versus Value

Mutability Matrix of Pain 12

• For variables defined in a method, the reference to the name is on the stack frame

• The value itself is still located elsewhere (possibly contiguous, no guarantee)

def myMethod() = {

val me = new Person("Jamie", "Allen");

...

}

Person("Jamie", "Allen")

me

Page 13: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Final

Mutability Matrix of Pain 13

• If we make a reference final in Java, it cannot be reassigned to a new value

• Easy to forget to use the keyword

• A mutable value remains mutable despite the usage of the finalkeyword

final Person me = new Person("Jamie", "Allen");

val me: Person = Person("Jamie", "Allen")

Page 14: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Mutability Matrix of Pain 14

The Mutability Matrix of Pain

val

var

ImmutableMutable

NAME

VALUE

@volatile

Page 15: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Implications of Immutability

Mutability Matrix of Pain 15

• Uses more Eden/NewGen heap space

• Resizing Eden/NewGen larger means longer stop the world pauses, may cause Survivor spillover directly into Tenured/OldGen

• Measure the effects of attempts to tune GC at varying loads

• Only optimize where you must, over 99% of my customers don't even try and are still wildly successful

Page 16: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Volatile is Your Friend

Mutability Matrix of Pain 16

• Use snapshots when mutability must be attained, and publish updates via volatile with the last write

• Readers should first read the one volatile field of multiple writes (reverse your order of access)

Page 17: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Caveat

• Just because you're using volatile doesn't mean that publishing cannot happen before you expect

• For greater consistency guarantees, volatile may not be appropriate

Mutability Matrix of Pain 17

Page 18: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

Use volatile!

• Don't be mutable, but if you have to, stay in the snapshot world with volatile and avoid locks

• Understand the visibility guarantees you need for your application, and consider the impact when you leave visibility to chance

• You can be more specific with read and write barriers if you go with sun.misc.Unsafe

Mutability Matrix of Pain 18

Page 19: @jamie allen - chariotsolutions.comchariotsolutions.com/.../04/JamieAllen_ETE2015_TheMutabilityMatri… · @jamie_allen Mutability Matrix of Pain 2. Agenda •The Java Memory Model

©Typesafe 2015 – All Rights Reserved