SlideShare a Scribd company logo
1 of 19
@jamie_allen
Mutability Matrix of Pain 2
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
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
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 Francisconi
http://www.researchbeta.com/?p=334
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
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
Concurrent and Parallel
Mutability Matrix of Pain 8
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");
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
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 collections
val 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
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
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 final
keyword
final Person me = new Person("Jamie", "Allen");
val me: Person = Person("Jamie", "Allen")
Mutability Matrix of Pain 14
The Mutability Matrix of Pain
val
var
ImmutableMutable
NAME
VALUE
@volatile
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
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)
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
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
©Typesafe 2015 – All Rights Reserved

More Related Content

More from shinolajla

20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rsshinolajla
 
20180416 reactive is_a_product
20180416 reactive is_a_product20180416 reactive is_a_product
20180416 reactive is_a_productshinolajla
 
20161027 scala io_keynote
20161027 scala io_keynote20161027 scala io_keynote
20161027 scala io_keynoteshinolajla
 
20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the tradeshinolajla
 
20160524 ibm fast data meetup
20160524 ibm fast data meetup20160524 ibm fast data meetup
20160524 ibm fast data meetupshinolajla
 
20160520 The Future of Services
20160520 The Future of Services20160520 The Future of Services
20160520 The Future of Servicesshinolajla
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdasshinolajla
 
20160317 lagom sf scala
20160317 lagom sf scala20160317 lagom sf scala
20160317 lagom sf scalashinolajla
 
Effective Akka v2
Effective Akka v2Effective Akka v2
Effective Akka v2shinolajla
 
Reactive applications tools of the trade huff po
Reactive applications   tools of the trade huff poReactive applications   tools of the trade huff po
Reactive applications tools of the trade huff poshinolajla
 
20140228 fp and_performance
20140228 fp and_performance20140228 fp and_performance
20140228 fp and_performanceshinolajla
 
Effective akka scalaio
Effective akka scalaioEffective akka scalaio
Effective akka scalaioshinolajla
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3shinolajla
 
Effective actors japanesesub
Effective actors japanesesubEffective actors japanesesub
Effective actors japanesesubshinolajla
 
Effective Actors
Effective ActorsEffective Actors
Effective Actorsshinolajla
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 

More from shinolajla (18)

20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs
 
20180416 reactive is_a_product
20180416 reactive is_a_product20180416 reactive is_a_product
20180416 reactive is_a_product
 
20161027 scala io_keynote
20161027 scala io_keynote20161027 scala io_keynote
20161027 scala io_keynote
 
20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the trade
 
20160524 ibm fast data meetup
20160524 ibm fast data meetup20160524 ibm fast data meetup
20160524 ibm fast data meetup
 
20160520 The Future of Services
20160520 The Future of Services20160520 The Future of Services
20160520 The Future of Services
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
 
20160317 lagom sf scala
20160317 lagom sf scala20160317 lagom sf scala
20160317 lagom sf scala
 
Effective Akka v2
Effective Akka v2Effective Akka v2
Effective Akka v2
 
Reactive applications tools of the trade huff po
Reactive applications   tools of the trade huff poReactive applications   tools of the trade huff po
Reactive applications tools of the trade huff po
 
20140228 fp and_performance
20140228 fp and_performance20140228 fp and_performance
20140228 fp and_performance
 
Effective akka scalaio
Effective akka scalaioEffective akka scalaio
Effective akka scalaio
 
Cpu Caches
Cpu CachesCpu Caches
Cpu Caches
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3
 
Effective actors japanesesub
Effective actors japanesesubEffective actors japanesesub
Effective actors japanesesub
 
Effective Actors
Effective ActorsEffective Actors
Effective Actors
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
CPU Caches
CPU CachesCPU Caches
CPU Caches
 

Recently uploaded

New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

20150411 mutability matrix of pain scala

  • 1.
  • 3. 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
  • 4. 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
  • 5. 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 Francisconi http://www.researchbeta.com/?p=334
  • 6. 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
  • 7. 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
  • 9. 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");
  • 10. 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
  • 11. 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 collections val 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
  • 12. 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
  • 13. 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 final keyword final Person me = new Person("Jamie", "Allen"); val me: Person = Person("Jamie", "Allen")
  • 14. Mutability Matrix of Pain 14 The Mutability Matrix of Pain val var ImmutableMutable NAME VALUE @volatile
  • 15. 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
  • 16. 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)
  • 17. 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
  • 18. 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
  • 19. ©Typesafe 2015 – All Rights Reserved