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")
...
}
me
Person( , , )
String("Jamie") String("Allen")
Collections
Mutability Matrix of Pain 11
• Collections are an aggregation of references to values
• Can be lots of “pointer chasing”
val me = new Person("Jamie", "Allen")
val friend = new Person("Jane", "Doe")
val us = List(me, friend)
Person( , , )
Person( , )
us
me
friend
String("Jamie") String("Allen")
String("Jane")
String("Doe")
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")
...
}
me
Person( , , )
String("Jamie") String("Allen")
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 Typesafe 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 JAXLondon_Conference

All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...
JAXLondon_Conference
 
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo EuteneuerStop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
JAXLondon_Conference
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
JAXLondon_Conference
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
JAXLondon_Conference
 
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay PrewerSmoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
JAXLondon_Conference
 
Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton
Java generics past, present and future - Raoul-Gabriel Urma, Richard WarburtonJava generics past, present and future - Raoul-Gabriel Urma, Richard Warburton
Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton
JAXLondon_Conference
 
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos ErotocritouHybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
JAXLondon_Conference
 

More from JAXLondon_Conference (20)

Cassandra and Spark - Tim Berglund
Cassandra and Spark - Tim BerglundCassandra and Spark - Tim Berglund
Cassandra and Spark - Tim Berglund
 
All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...
 
The Unit Test is dead. Long live the Unit Test! - Colin Vipurs
The Unit Test is dead. Long live the Unit Test! - Colin VipursThe Unit Test is dead. Long live the Unit Test! - Colin Vipurs
The Unit Test is dead. Long live the Unit Test! - Colin Vipurs
 
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo EuteneuerStop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
 
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay PrewerSmoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
 
VC from the inside - a techie's perspective - Adrian Colyer
VC from the inside - a techie's perspective - Adrian ColyerVC from the inside - a techie's perspective - Adrian Colyer
VC from the inside - a techie's perspective - Adrian Colyer
 
Use your type system; write less code - Samir Talwar
Use your type system; write less code - Samir TalwarUse your type system; write less code - Samir Talwar
Use your type system; write less code - Samir Talwar
 
Thinking fast and slow with software development - Daniel Bryant
Thinking fast and slow with software development - Daniel BryantThinking fast and slow with software development - Daniel Bryant
Thinking fast and slow with software development - Daniel Bryant
 
The art of shifting perspectives - Rachel Davies
The art of shifting perspectives - Rachel DaviesThe art of shifting perspectives - Rachel Davies
The art of shifting perspectives - Rachel Davies
 
Spring Boot in the Web Tier - Dave Syer
Spring Boot in the Web Tier - Dave SyerSpring Boot in the Web Tier - Dave Syer
Spring Boot in the Web Tier - Dave Syer
 
Microservices from dream to reality in an hour - Dr. Holly Cummins
Microservices from dream to reality in an hour - Dr. Holly CumminsMicroservices from dream to reality in an hour - Dr. Holly Cummins
Microservices from dream to reality in an hour - Dr. Holly Cummins
 
Love your architecture - Alexander von Zitzewitz
Love your architecture - Alexander von ZitzewitzLove your architecture - Alexander von Zitzewitz
Love your architecture - Alexander von Zitzewitz
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Java vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris BaileyJava vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris Bailey
 
Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton
Java generics past, present and future - Raoul-Gabriel Urma, Richard WarburtonJava generics past, present and future - Raoul-Gabriel Urma, Richard Warburton
Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton
 
Java 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen ColebourneJava 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen Colebourne
 
Intuitions for scaling data centric architectures - Benjamin Stopford
Intuitions for scaling data centric architectures - Benjamin StopfordIntuitions for scaling data centric architectures - Benjamin Stopford
Intuitions for scaling data centric architectures - Benjamin Stopford
 
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos ErotocritouHybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 

The java memory model and the mutability matrix of pain - Jamie Allen

  • 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") ... } me Person( , , ) String("Jamie") String("Allen")
  • 11. Collections Mutability Matrix of Pain 11 • Collections are an aggregation of references to values • Can be lots of “pointer chasing” val me = new Person("Jamie", "Allen") val friend = new Person("Jane", "Doe") val us = List(me, friend) Person( , , ) Person( , ) us me friend String("Jamie") String("Allen") String("Jane") String("Doe")
  • 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") ... } me Person( , , ) String("Jamie") String("Allen")
  • 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 Typesafe 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