SlideShare a Scribd company logo
Deep Dive:
Memory Management in Apache
Andrew Or
May 18th, 2016
@andrewor14
How familiar are you with Apache Spark?
a) I contribute to it
b) I use it in production
c) I am evaluating it
d) I have nothing to do with it
2
What is Apache ?
3
Fast and general engine for big data processing
Fast to run code
– In-memory data sharing
– General computation graphs
Fast to write code
– Rich APIs in Java, Scala, Python
– Interactive shell
4
Spark Core
Spark
Streaming
real-time
Spark SQL
structured data
MLlib
machine
learning
GraphX
graph
…
What is Apache ?
About Databricks
5
Team that created Spark
at UC Berkeley
Offer a hosted service
– Spark in the cloud
– Notebooks
– Plot visualizations
– Cluster management
About Me
6
Apache Spark committer
Software eng @ Databricks
Hadoop Summit ‘15
Spark Summit Europe ‘15
Some other meetup talks
7
Efficient memory use is
critical to good performance
Memory Management in Apache Spark
Memory contention poses three
challenges for Apache Spark
9
How to arbitrate memory between execution and storage?
How to arbitrate memory across tasks running in parallel?
How to arbitrate memory across operators running within
the same task?
Two usages of memory in Apache Spark
10
Execution
Memory used for shuffles, joins, sorts and aggregations
Storage
Memory used to cache data that will be reused later
Iterator
4, 3, 5, 1, 6, 2, 8
4 3 5 1 6 2 8
Sort
4 3 5 1 6 2 8
Sort
Execution memory
Iterator
1, 2, 3, 4, 5, 6, 8
1 2 3 4 5 6 8
Sort
Execution memory
Iterator Map Iterator
1, 2, 3, 4, 5, 6, 8 +1 2, 3, 4, 5, 6, 7, 9
Iterator Map
1, 2, 3, 4, 5, 6, 8 +1
Map
+1
Iterator
1, 2, 3, 4, 5, 6, 8
...
Iterator
2, 3, 4, 5, 6, 7, 9
Iterator
2, 3, 4, 5, 6, 7, 9
Cached
Iterator
1, 2, 3, 4, 5, 6, 8
Map Iterator
Map Iterator
...
Storage
memory
Map
+1
Iterator
2, 3, 4, 5, 6, 7, 9
Challenge #1
How to arbitrate memory between
execution and storage?
Easy, static allocation!
18
Total available memory
Execution Storage
Spark 1.0
May 2014
Easy, static allocation!
19
Execution Storage
Spill to disk
Spark 1.0
May 2014
Easy, static allocation!
20
Execution Storage
Spark 1.0
May 2014
Easy, static allocation!
21
Execution Storage
Evict LRU block to disk
Spark 1.0
May 2014
22
Inefficient memory use means
bad performance
Easy, static allocation!
23
Execution can only use a fraction of the memory,
even when there is no storage!
Execution Storage
Spark 1.0May 2014
Storage
Easy, static allocation!
24
Efficient use of memory required user tuning
Execution
Spark 1.0May 2014
25
Fast forward to 2016…
How could we have done better?
26
Execution Storage
27
Execution Storage
Unified memory management
Spark 1.6+
Jan 2016
What happens if there is already storage?
28
Execution Storage
Unified memory management
Spark 1.6+
Jan 2016
Evict LRU block to disk
29
Execution Storage
Unified memory management
Spark 1.6+
Jan 2016
What about the other way round?
30
Execution Storage
Unified memory management
Spark 1.6+
Jan 2016
Evict LRU block to disk
Design considerations
31
Why evict storage, not execution?
Spilled execution data will always be read back from disk,
whereas cached data may not.
What if the application relies on caching?
Allow the user to specify a minimum unevictable amount of
cached data (not a reservation!).
Spark 1.6+
Jan 2016
Challenge #2
How to arbitrate memory across
tasks running in parallel?
Easy, static allocation!
Worker machine has 4 cores
Each task gets 1/4 of the total memory
Slot 1 Slot 2 Slot 3 Slot 4
Alternative: What Spark does
Worker machine has 4 cores
The share of each task depends on
number of actively running tasks (N)
Task 1
Alternative: What Spark does
Now, another task comes along
so the first task will have to spill
Task 1
Alternative: What Spark does
Each task is assigned 1/N of the
memory, where N = 2
Task 1 Task 2
Alternative: What Spark does
Each task is assigned 1/N of the
memory, where N = 4
Task 1 Task 2 Task 3 Task 4
Alternative: What Spark does
Last remaining task gets all the
memory because N = 1
Task 3
Spark 1.0+
May 2014
Static allocation vs What Spark does
39
Both are fair and starvation free
Static allocation is simpler
What Spark does handles stragglers better
Challenge #3
How to arbitrate memory across
operators running within the same task?
SELECT age, avg(height)
FROM students
GROUP BY age
ORDER BY avg(height)
students.groupBy("age")
.avg("height")
.orderBy("avg(height)")
.collect()
Scan
Project
Aggregate
Sort
Worker has 6
pages of memory
Scan
Project
Aggregate
Sort
Scan
Project
Aggregate
Sort
Map { // age → heights
20 → [154, 174, 175]
21 → [167, 168, 181]
22 → [155, 166, 188]
23 → [160, 168, 178, 183]
}
Scan
Project
Aggregate
Sort
All 6 pages were used
by Aggregate, leaving
no memory for Sort!
Solution #1:
Reserve a page for
each operator
Scan
Project
Aggregate
Sort
Solution #1:
Reserve a page for
each operator
Scan
Project
Aggregate
Sort
Starvation free, but still not fair…
What if there were more operators?
Solution #2:
Cooperative spilling
Scan
Project
Aggregate
Sort
Scan
Project
Aggregate
Sort
Solution #2:
Cooperative spilling
Scan
Project
Aggregate
Sort
Solution #2:
Cooperative spilling
Sort forces Aggregate to spill
a page to free memory
Scan
Project
Aggregate
Sort
Solution #2:
Cooperative spilling
Sort needs more memory so
it forces Aggregate to spill
another page (and so on)
Scan
Project
Aggregate
Sort
Solution #2:
Cooperative spilling
Sort finishes with 3 pages
Aggregate does not have to
spill its remaining pages
Spark 1.6+
Jan 2016
Recap: Three sources of contention
52
How to arbitrate memory …
● between execution and storage?
● across tasks running in parallel?
● across operators running within the same task?
Instead of avoid statically reserving memory in advance, deal with
memory contention when it arises by forcing members to spill
Project Tungsten
53
Binary in-memory data representation
Cache-aware computation
Code generation (next time)
Spark 1.4+
Jun 2015
“abcd”
54
• Native: 4 bytes with UTF-8 encoding
• Java: 48 bytes
– 12 byte header
– 2 bytes per character (UTF-16 internal representation)
– 20 bytes of additional overhead
– 8 byte hash code
Java objects have large overheads
55
Schema: (Int, String, string)
Row
Array String(“data”)
String(“bricks”)
5+ objects, high space overhead, expensive hashCode()
BoxedInteger(123)
Java objects based row format
6 “bricks”
56
0x0 123 32L 48L 4 “data”
(123, “data”, “bricks”)
Null tracking bitmap
Offset to var. length data
Offset to var. length data
Tungsten row format
Cache-aware Computation
57
ptr key rec
ptr key rec
ptr key rec
Naive layout
Poor cache locality
ptrkey prefix rec
ptrkey prefix rec
ptrkey prefix rec
Cache-aware layout
Good cache locality
E.g. sorting a list of records
For more info...
Deep Dive into Project Tungsten: Bringing Spark Closer to Bare Metal
https://www.youtube.com/watch?v=5ajs8EIPWGI
Spark Performance: What’s Next
https://www.youtube.com/watch?v=JX0CdOTWYX4
Unified Memory Management
https://issues.apache.org/jira/browse/SPARK-10000
Thank you
andrew@databricks.com
@andrewor14

More Related Content

PDF
Deep Dive: Memory Management in Apache Spark
PPTX
Spark Shuffle Deep Dive (Explained In Depth) - How Shuffle Works in Spark
PDF
Apache Spark Core—Deep Dive—Proper Optimization
PDF
Deep Dive into Spark SQL with Advanced Performance Tuning with Xiao Li & Wenc...
PDF
Top 5 Mistakes to Avoid When Writing Apache Spark Applications
PDF
Understanding Query Plans and Spark UIs
PDF
Fine Tuning and Enhancing Performance of Apache Spark Jobs
PDF
Introduction to Spark Internals
Deep Dive: Memory Management in Apache Spark
Spark Shuffle Deep Dive (Explained In Depth) - How Shuffle Works in Spark
Apache Spark Core—Deep Dive—Proper Optimization
Deep Dive into Spark SQL with Advanced Performance Tuning with Xiao Li & Wenc...
Top 5 Mistakes to Avoid When Writing Apache Spark Applications
Understanding Query Plans and Spark UIs
Fine Tuning and Enhancing Performance of Apache Spark Jobs
Introduction to Spark Internals

What's hot (20)

PDF
Apache Spark Core – Practical Optimization
PDF
Top 5 mistakes when writing Spark applications
PDF
Spark shuffle introduction
PDF
Adaptive Query Execution: Speeding Up Spark SQL at Runtime
PDF
Deep Dive into Project Tungsten: Bringing Spark Closer to Bare Metal-(Josh Ro...
PDF
Top 5 Mistakes When Writing Spark Applications
PDF
Physical Plans in Spark SQL
PDF
A Deep Dive into Query Execution Engine of Spark SQL
PPTX
Processing Large Data with Apache Spark -- HasGeek
PDF
Understanding Memory Management In Spark For Fun And Profit
PPTX
Optimizing Apache Spark SQL Joins
PPTX
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
PPTX
Evening out the uneven: dealing with skew in Flink
PDF
Apache Spark in Depth: Core Concepts, Architecture & Internals
PDF
Spark + Parquet In Depth: Spark Summit East Talk by Emily Curtin and Robbie S...
PDF
Parquet performance tuning: the missing guide
PDF
Tuning Apache Spark for Large-Scale Workloads Gaoxiang Liu and Sital Kedia
PDF
The Parquet Format and Performance Optimization Opportunities
PPTX
Spark architecture
Apache Spark Core – Practical Optimization
Top 5 mistakes when writing Spark applications
Spark shuffle introduction
Adaptive Query Execution: Speeding Up Spark SQL at Runtime
Deep Dive into Project Tungsten: Bringing Spark Closer to Bare Metal-(Josh Ro...
Top 5 Mistakes When Writing Spark Applications
Physical Plans in Spark SQL
A Deep Dive into Query Execution Engine of Spark SQL
Processing Large Data with Apache Spark -- HasGeek
Understanding Memory Management In Spark For Fun And Profit
Optimizing Apache Spark SQL Joins
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
Evening out the uneven: dealing with skew in Flink
Apache Spark in Depth: Core Concepts, Architecture & Internals
Spark + Parquet In Depth: Spark Summit East Talk by Emily Curtin and Robbie S...
Parquet performance tuning: the missing guide
Tuning Apache Spark for Large-Scale Workloads Gaoxiang Liu and Sital Kedia
The Parquet Format and Performance Optimization Opportunities
Spark architecture
Ad

Viewers also liked (20)

PDF
700 Queries Per Second with Updates: Spark As A Real-Time Web Service
PDF
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
PDF
Why your Spark job is failing
PDF
Visualizing AutoTrader Traffic in Near Real-Time with Spark Streaming-(Jon Gr...
PDF
Visualizing big data in the browser using spark
PPTX
Apache HAWQ Architecture
PDF
Beyond SQL: Speeding up Spark with DataFrames
PPTX
TensorFrames: Google Tensorflow on Apache Spark
PPTX
Real time data viz with Spark Streaming, Kafka and D3.js
PDF
R, Scikit-Learn and Apache Spark ML - What difference does it make?
PDF
Lessons from Running Large Scale Spark Workloads
PPTX
Spark Infrastructure Made Easy
PDF
Jump Start with Apache Spark 2.0 on Databricks
PDF
Deep Dive Into Catalyst: Apache Spark 2.0'S Optimizer
PDF
Interactive Visualization of Streaming Data Powered by Spark by Ruhollah Farc...
PDF
Apache Spark: What's under the hood
PPTX
SORT & JOIN IN SPARK 2.0
PDF
Apps to spark memory
PDF
Making Sense of Spark Performance-(Kay Ousterhout, UC Berkeley)
PDF
Spark performance tuning - Maksud Ibrahimov
700 Queries Per Second with Updates: Spark As A Real-Time Web Service
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Why your Spark job is failing
Visualizing AutoTrader Traffic in Near Real-Time with Spark Streaming-(Jon Gr...
Visualizing big data in the browser using spark
Apache HAWQ Architecture
Beyond SQL: Speeding up Spark with DataFrames
TensorFrames: Google Tensorflow on Apache Spark
Real time data viz with Spark Streaming, Kafka and D3.js
R, Scikit-Learn and Apache Spark ML - What difference does it make?
Lessons from Running Large Scale Spark Workloads
Spark Infrastructure Made Easy
Jump Start with Apache Spark 2.0 on Databricks
Deep Dive Into Catalyst: Apache Spark 2.0'S Optimizer
Interactive Visualization of Streaming Data Powered by Spark by Ruhollah Farc...
Apache Spark: What's under the hood
SORT & JOIN IN SPARK 2.0
Apps to spark memory
Making Sense of Spark Performance-(Kay Ousterhout, UC Berkeley)
Spark performance tuning - Maksud Ibrahimov
Ad

Similar to Memory Management in Apache Spark (20)

PDF
Re-Architecting Spark For Performance Understandability
PDF
Re-Architecting Spark For Performance Understandability
PDF
Spark Summit EU talk by Qifan Pu
PDF
Apache Spark Performance: Past, Future and Present
PDF
Top 5 mistakes when writing Spark applications
PDF
夏俊鸾:Spark——基于内存的下一代大数据分析框架
PPTX
Control dataset partitioning and cache to optimize performances in Spark
PDF
Spark on YARN
PDF
Tachyon-2014-11-21-amp-camp5
PDF
Building a Unified Data Pipeline with Apache Spark and XGBoost with Nan Zhu
PPTX
Spark Tips & Tricks
PDF
GraphChi big graph processing
PDF
Introduction to Parallelization ans performance optimization
PDF
Exploiting GPUs in Spark
PDF
Scaling Apache Spark at Facebook
PDF
Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
PPTX
Introduction to Parallelization ans performance optimization
PDF
Architecting and productionising data science applications at scale
PDF
Top 5 mistakes when writing Spark applications
PDF
Apache Spark Performance tuning and Best Practise
Re-Architecting Spark For Performance Understandability
Re-Architecting Spark For Performance Understandability
Spark Summit EU talk by Qifan Pu
Apache Spark Performance: Past, Future and Present
Top 5 mistakes when writing Spark applications
夏俊鸾:Spark——基于内存的下一代大数据分析框架
Control dataset partitioning and cache to optimize performances in Spark
Spark on YARN
Tachyon-2014-11-21-amp-camp5
Building a Unified Data Pipeline with Apache Spark and XGBoost with Nan Zhu
Spark Tips & Tricks
GraphChi big graph processing
Introduction to Parallelization ans performance optimization
Exploiting GPUs in Spark
Scaling Apache Spark at Facebook
Scalable Monitoring Using Prometheus with Apache Spark Clusters with Diane F...
Introduction to Parallelization ans performance optimization
Architecting and productionising data science applications at scale
Top 5 mistakes when writing Spark applications
Apache Spark Performance tuning and Best Practise

More from Databricks (20)

PPTX
DW Migration Webinar-March 2022.pptx
PPTX
Data Lakehouse Symposium | Day 1 | Part 1
PPT
Data Lakehouse Symposium | Day 1 | Part 2
PPTX
Data Lakehouse Symposium | Day 2
PPTX
Data Lakehouse Symposium | Day 4
PDF
5 Critical Steps to Clean Your Data Swamp When Migrating Off of Hadoop
PDF
Democratizing Data Quality Through a Centralized Platform
PDF
Learn to Use Databricks for Data Science
PDF
Why APM Is Not the Same As ML Monitoring
PDF
The Function, the Context, and the Data—Enabling ML Ops at Stitch Fix
PDF
Stage Level Scheduling Improving Big Data and AI Integration
PDF
Simplify Data Conversion from Spark to TensorFlow and PyTorch
PDF
Scaling your Data Pipelines with Apache Spark on Kubernetes
PDF
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
PDF
Sawtooth Windows for Feature Aggregations
PDF
Redis + Apache Spark = Swiss Army Knife Meets Kitchen Sink
PDF
Re-imagine Data Monitoring with whylogs and Spark
PDF
Raven: End-to-end Optimization of ML Prediction Queries
PDF
Processing Large Datasets for ADAS Applications using Apache Spark
PDF
Massive Data Processing in Adobe Using Delta Lake
DW Migration Webinar-March 2022.pptx
Data Lakehouse Symposium | Day 1 | Part 1
Data Lakehouse Symposium | Day 1 | Part 2
Data Lakehouse Symposium | Day 2
Data Lakehouse Symposium | Day 4
5 Critical Steps to Clean Your Data Swamp When Migrating Off of Hadoop
Democratizing Data Quality Through a Centralized Platform
Learn to Use Databricks for Data Science
Why APM Is Not the Same As ML Monitoring
The Function, the Context, and the Data—Enabling ML Ops at Stitch Fix
Stage Level Scheduling Improving Big Data and AI Integration
Simplify Data Conversion from Spark to TensorFlow and PyTorch
Scaling your Data Pipelines with Apache Spark on Kubernetes
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
Sawtooth Windows for Feature Aggregations
Redis + Apache Spark = Swiss Army Knife Meets Kitchen Sink
Re-imagine Data Monitoring with whylogs and Spark
Raven: End-to-end Optimization of ML Prediction Queries
Processing Large Datasets for ADAS Applications using Apache Spark
Massive Data Processing in Adobe Using Delta Lake

Recently uploaded (20)

PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
assetexplorer- product-overview - presentation
PPT
Introduction Database Management System for Course Database
PDF
Digital Strategies for Manufacturing Companies
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
top salesforce developer skills in 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Odoo Companies in India – Driving Business Transformation.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Digital Systems & Binary Numbers (comprehensive )
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How to Choose the Right IT Partner for Your Business in Malaysia
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Navsoft: AI-Powered Business Solutions & Custom Software Development
L1 - Introduction to python Backend.pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
assetexplorer- product-overview - presentation
Introduction Database Management System for Course Database
Digital Strategies for Manufacturing Companies
Upgrade and Innovation Strategies for SAP ERP Customers
top salesforce developer skills in 2025.pdf
ai tools demonstartion for schools and inter college
Operating system designcfffgfgggggggvggggggggg
Which alternative to Crystal Reports is best for small or large businesses.pdf

Memory Management in Apache Spark