SlideShare a Scribd company logo
The java memory model and the mutability matrix of pain - Jamie Allen
@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

PPTX
20150411 mutability matrix of pain scala
PPSX
Execution flow of main() method inside jvm
PPTX
Java concurrency
PPTX
Jvm memory model
PPT
Clojure slides
PDF
Practical Introduction to Java Memory Model
PDF
jvm/java - towards lock-free concurrency
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
20150411 mutability matrix of pain scala
Execution flow of main() method inside jvm
Java concurrency
Jvm memory model
Clojure slides
Practical Introduction to Java Memory Model
jvm/java - towards lock-free concurrency
If You Think You Can Stay Away from Functional Programming, You Are Wrong

Similar to The java memory model and the mutability matrix of pain - Jamie Allen (20)

PDF
API Design
PDF
Value Objects
PPTX
The Java memory model made easy
PPTX
Programming picaresque
PPTX
JVM Memory Model - Yoav Abrahami, Wix
ODP
Advanced java
PDF
Study effective java item 78 synchronize access to mutable data
PDF
Comparing different concurrency models on the JVM
PPTX
Николай Папирный Тема: "Java memory model для простых смертных"
PPTX
Introduction to value types
PDF
Functional Programming with Immutable Data Structures
PDF
Persistent Data Structures by @aradzie
PDF
"Java memory model for practitioners" at JavaLand 2017 by Vadym Kazulkin/Rodi...
PPTX
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
PDF
Collections forceawakens
PPT
Java findamentals1
PPT
Java findamentals1
PPT
Java findamentals1
ODP
Java Garbage Collection, Monitoring, and Tuning
PDF
COMP522-2019-Java-Memory-Model.pdf
API Design
Value Objects
The Java memory model made easy
Programming picaresque
JVM Memory Model - Yoav Abrahami, Wix
Advanced java
Study effective java item 78 synchronize access to mutable data
Comparing different concurrency models on the JVM
Николай Папирный Тема: "Java memory model для простых смертных"
Introduction to value types
Functional Programming with Immutable Data Structures
Persistent Data Structures by @aradzie
"Java memory model for practitioners" at JavaLand 2017 by Vadym Kazulkin/Rodi...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collections forceawakens
Java findamentals1
Java findamentals1
Java findamentals1
Java Garbage Collection, Monitoring, and Tuning
COMP522-2019-Java-Memory-Model.pdf
Ad

More from JAXLondon_Conference (20)

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

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPT
Introduction Database Management System for Course Database
PPTX
Introduction to Artificial Intelligence
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Digital Strategies for Manufacturing Companies
PPTX
assetexplorer- product-overview - presentation
Softaken Excel to vCard Converter Software.pdf
history of c programming in notes for students .pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
L1 - Introduction to python Backend.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Systems & Binary Numbers (comprehensive )
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
VVF-Customer-Presentation2025-Ver1.9.pptx
Introduction Database Management System for Course Database
Introduction to Artificial Intelligence
PTS Company Brochure 2025 (1).pdf.......
2025 Textile ERP Trends: SAP, Odoo & Oracle
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Reimagine Home Health with the Power of Agentic AI​
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Choose the Right IT Partner for Your Business in Malaysia
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Digital Strategies for Manufacturing Companies
assetexplorer- product-overview - presentation

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

  • 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