SlideShare una empresa de Scribd logo
1 de 49
Descargar para leer sin conexión
Parallel Ruby: Managing the
Memory Monster
Kevin Miller kevin.miller@flexport.com
We run a lot of background jobs
The Great Migration
Old
● 240 Ruby Processes
● 20 Servers
● 12 Processes per Server
● 1 Thread per Process
The Great Migration
Old
● 240 Ruby Processes
● 20 Servers
● 12 Processes per Server
● 1 Thread per Process
New
● 400 Ruby Threads
● 10 Servers
● 4 Processes per Server
● 10 Threads per Processes
Things started getting… slow
Side Lesson
If you deploy every 10 minutes, you can get away with a lot
And now back to business
Ruby Heap: 40 byte slots
Ruby Heap: 40 byte slots
“String”
Ruby Heap: 40 byte slots
“String”
Class.new
Ruby Heap: 40 byte slots
“String”
Class.new
{a: 2}
Ruby Heap: 40 byte slots
“String”
Class.new
{a: 2}
[1]
Ruby Heap: 40 byte slots
“String” 12132425
Class.new
{a: 2}
[1]
Ruby Heap: 40 byte slots
“String” 12132425
Class.new 121325.224
{a: 2}
[1]
Ruby Heap: 40 byte slots
“String” 12132425
Class.new 121325.224
{a: 2} /regular?/
[1]
Ruby Heap: 40 byte slots
“String” 12132425
Class.new 121325.224
{a: 2} /regular?/
[1] ...
“String” 12132425
Class.new 121325.224
{a: 2} /regular?/
[1] ...
“String” 12132425 [“aaaaaaaaa...
Class.new 121325.224
{a: 2} /regular?/
[1] ...
“String” 12132425 [“aaaaaaaaa...
Class.new 121325.224
{a: 2} /regular?/
[1] ...
C (OS) Heap Pages: 4KB of space per page
Big ArrayBig String
“String” 12132425 ArraySlot
Class.new 121325.224
{a: 2} /regular?/
[1] ...
Big
Array
Big
String
[String, String...]
“String” 12132425 ArraySlot “aaaa…”
Class.new 121325.224 “aaaa…” “aaaa…”
{a: 2} /regular?/ “aaaa…” “aaaa…”
[1] ... “aaaa…” “aaaa…”
Big
Array
Big
String
[String, String...]
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“String” 12132425 ArraySlot “aaaa…”
Class.new 121325.224 “aaaa…” “aaaa…”
{a: 2} /regular?/ “aaaa…” “aaaa…”
[1] ... “aaaa…” “aaaa…”
Big
Array
Big
String
[String, String...]
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“String” 12132425 ArraySlot StringSlot
Class.new 121325.224 StringSlot StringSlot
{a: 2} /regular?/ StringSlot StringSlot
[1] ... StringSlot StringSlot
Big
Array
Big
String
[String, String...]
Big
Array
Big
String
[String, String...]
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“String” 12132425 ArraySlot StringSlot
Class.new 121325.224 StringSlot StringSlot
{a: 2} /regular?/ StringSlot StringSlot
[1] ... StringSlot StringSlot
Big
Array
Big
String
[String, String...]
Total Slots = 1 for Array + 1024 for the 1024 strings = 1025
Total Ruby Heap Size = 1025 Slots * 40 bytes per slot = ~40KB = ~ .04 MB
Total OS Heap Size =
4 bytes per array elem * 1024 array elems + 1024 bytes per string * 1024 strings =
= 1028KB = ~1MB
We expect memory to be around here
Big
Array
Big
String
[String, String...]
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“String” 12132425 ArraySlot StringSlot
Class.new 121325.224 StringSlot StringSlot
{a: 2} /regular?/ StringSlot StringSlot
[1] ... StringSlot StringSlot
Big
Array
Big
String
[String, String...]
Big
Array
Big
String
[String, String...]
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“aaaa…” “aaaa…” “aaaa…” “aaaa…”
“String” 12132425 ArraySlot StringSlot
Class.new 121325.224 StringSlot StringSlot
{a: 2} /regular?/ StringSlot StringSlot
[1] ... StringSlot StringSlot
Big Array
Big
String Wasted
Space
Ruby Threads Are Not Memory Friendly
Thread 1
Fragmented
OS Heap 1
Thread 1
Fragmented
OS Heap 1
Thread 2
Thread 1
Fragmented
OS Heap 1
Thread 2
C Default: One OS Heap per Thread
Thread 1 Thread 2
Fragmented
OS Heap 1
Fragmented
OS Heap 2
C Default: One OS Heap per Thread
Thread 1 Thread 2 Thread 20
...
Fragmented
OS Heap 1
Fragmented
OS Heap 2
Fragmented
OS Heap 20
Option 1: Reduce Number of OS Heaps
Number of OS Heaps (a.k.a Arenas) can be configured with
MALLOC_ARENA_MAX
We expect memory to be around here
Option 2: Use a Different Memory Allocator
Can switch to jemalloc* over standard glibc malloc with
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
* After installing it via homebrew/apt-get/yum/etc
We expect memory to be around here
So… What should you do?
Default Reduced Arena Count jemalloc
Pros Out of the Box Works with all ruby
Significant memory
reduction
Best Memory Usage
Cons Heavy Fragmentation Increases contention in
ruby implementations
with true threading
(JRuby)
Requires reinstalling
ruby and installing
jemalloc
Use Cases Single-threaded Ruby
Programs on OS’s you
don’t control
Can set environment
variables but cannot
install packages
Anytime you’re able to
So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
“I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is
terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a
miracle occurs: their memory usage drops massively”
Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of
standard Ruby
So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
“I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is
terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a
miracle occurs: their memory usage drops massively”
“On Ubuntu Linux, with Rails Ruby Bench, jemalloc gives an overall
speedup (not just memory, end-to-end speedup) of around 11%”
Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of
standard Ruby
So… What should you do?
1) If you control the environment, jemalloc is king
2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of
standard Ruby
“I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is
terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a
miracle occurs: their memory usage drops massively”
“On Ubuntu Linux, with Rails Ruby Bench, jemalloc gives an overall
speedup (not just memory, end-to-end speedup) of around 11%”
“I'm another Ruby user that used to have memory bloat problems and
switched to jemalloc as well”
Want to Learn More?
Go to Aaron Patterson’s Talk:
“Compacting Heaps in Ruby 2.7”

Más contenido relacionado

La actualidad más candente

The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018Charles Nutter
 
Don’t block the event loop!
Don’t block the event loop!Don’t block the event loop!
Don’t block the event loop!hujinpu
 
Cassandra SF Meetup - CQL Performance With Apache Cassandra 3.X
Cassandra SF Meetup - CQL Performance With Apache Cassandra 3.XCassandra SF Meetup - CQL Performance With Apache Cassandra 3.X
Cassandra SF Meetup - CQL Performance With Apache Cassandra 3.Xaaronmorton
 
NYC Cassandra Day - Java Intro
NYC Cassandra Day - Java IntroNYC Cassandra Day - Java Intro
NYC Cassandra Day - Java IntroChristopher Batey
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Alexey Fyodorov
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in PythonMosky Liu
 
Beginners guide-concurrency
Beginners guide-concurrencyBeginners guide-concurrency
Beginners guide-concurrencyMichael Barker
 
Manchester Hadoop Meetup: Cassandra Spark internals
Manchester Hadoop Meetup: Cassandra Spark internalsManchester Hadoop Meetup: Cassandra Spark internals
Manchester Hadoop Meetup: Cassandra Spark internalsChristopher Batey
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展Leon Chen
 
Fight with Metaspace OOM
Fight with Metaspace OOMFight with Metaspace OOM
Fight with Metaspace OOMLeon Chen
 
LMAX Disruptor as real-life example
LMAX Disruptor as real-life exampleLMAX Disruptor as real-life example
LMAX Disruptor as real-life exampleGuy Nir
 
Мониторинг. Опять, rootconf 2016
Мониторинг. Опять, rootconf 2016Мониторинг. Опять, rootconf 2016
Мониторинг. Опять, rootconf 2016Vsevolod Polyakov
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseChristian Melchior
 
LJC: Microservices in the real world
LJC: Microservices in the real worldLJC: Microservices in the real world
LJC: Microservices in the real worldChristopher Batey
 
How to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHiroshi SHIBATA
 
Coroutines talk ppt
Coroutines talk pptCoroutines talk ppt
Coroutines talk pptShahroz Khan
 
KOTLIN COROUTINES - PART 1
KOTLIN COROUTINES - PART 1KOTLIN COROUTINES - PART 1
KOTLIN COROUTINES - PART 1Bartosz Kozajda
 

La actualidad más candente (20)

The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018
 
Don’t block the event loop!
Don’t block the event loop!Don’t block the event loop!
Don’t block the event loop!
 
Cassandra SF Meetup - CQL Performance With Apache Cassandra 3.X
Cassandra SF Meetup - CQL Performance With Apache Cassandra 3.XCassandra SF Meetup - CQL Performance With Apache Cassandra 3.X
Cassandra SF Meetup - CQL Performance With Apache Cassandra 3.X
 
NYC Cassandra Day - Java Intro
NYC Cassandra Day - Java IntroNYC Cassandra Day - Java Intro
NYC Cassandra Day - Java Intro
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
Run Node Run
Run Node RunRun Node Run
Run Node Run
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in Python
 
Beginners guide-concurrency
Beginners guide-concurrencyBeginners guide-concurrency
Beginners guide-concurrency
 
Manchester Hadoop Meetup: Cassandra Spark internals
Manchester Hadoop Meetup: Cassandra Spark internalsManchester Hadoop Meetup: Cassandra Spark internals
Manchester Hadoop Meetup: Cassandra Spark internals
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 
Mesos Networking
Mesos NetworkingMesos Networking
Mesos Networking
 
Fight with Metaspace OOM
Fight with Metaspace OOMFight with Metaspace OOM
Fight with Metaspace OOM
 
LMAX Disruptor as real-life example
LMAX Disruptor as real-life exampleLMAX Disruptor as real-life example
LMAX Disruptor as real-life example
 
Мониторинг. Опять, rootconf 2016
Мониторинг. Опять, rootconf 2016Мониторинг. Опять, rootconf 2016
Мониторинг. Опять, rootconf 2016
 
Metrics: where and how
Metrics: where and howMetrics: where and how
Metrics: where and how
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
LJC: Microservices in the real world
LJC: Microservices in the real worldLJC: Microservices in the real world
LJC: Microservices in the real world
 
How to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby Core
 
Coroutines talk ppt
Coroutines talk pptCoroutines talk ppt
Coroutines talk ppt
 
KOTLIN COROUTINES - PART 1
KOTLIN COROUTINES - PART 1KOTLIN COROUTINES - PART 1
KOTLIN COROUTINES - PART 1
 

Similar a Parallel Ruby: Managing the Memory Monster

Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - DeploymentFabio Akita
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilitiesnagachika t
 
Top 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applicationsTop 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applicationsmarkgrover
 
Rocket Fuelled Cucumbers
Rocket Fuelled CucumbersRocket Fuelled Cucumbers
Rocket Fuelled CucumbersJoseph Wilk
 
O que tem de novo no Ruby 2.0?
O que tem de novo no Ruby 2.0?O que tem de novo no Ruby 2.0?
O que tem de novo no Ruby 2.0?Fabio Akita
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5Peter Lawrey
 
Top 5 Mistakes When Writing Spark Applications
Top 5 Mistakes When Writing Spark ApplicationsTop 5 Mistakes When Writing Spark Applications
Top 5 Mistakes When Writing Spark ApplicationsSpark Summit
 
Top 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applicationsTop 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applicationshadooparchbook
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey J On The Beach
 
Tomcatx troubleshooting-production
Tomcatx troubleshooting-productionTomcatx troubleshooting-production
Tomcatx troubleshooting-productionVladimir Khokhryakov
 
(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task Execution(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task ExecutionAmazon Web Services
 
The Future of library dependency manageement of Ruby
The Future of library dependency manageement of RubyThe Future of library dependency manageement of Ruby
The Future of library dependency manageement of RubyHiroshi SHIBATA
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunningguest1f2740
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance TunningTerry Cho
 
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Paolo Negri
 
TorqueBox for Rubyists
TorqueBox for RubyistsTorqueBox for Rubyists
TorqueBox for Rubyistsbobmcwhirter
 
Memory Issues in Ruby on Rails Applications
Memory Issues in Ruby on Rails ApplicationsMemory Issues in Ruby on Rails Applications
Memory Issues in Ruby on Rails ApplicationsSimeon Simeonov
 
Matt Franklin - Apache Software (Geekfest)
Matt Franklin - Apache Software (Geekfest)Matt Franklin - Apache Software (Geekfest)
Matt Franklin - Apache Software (Geekfest)W2O Group
 

Similar a Parallel Ruby: Managing the Memory Monster (20)

Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - Deployment
 
All bugfixes are incompatibilities
All bugfixes are incompatibilitiesAll bugfixes are incompatibilities
All bugfixes are incompatibilities
 
Top 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applicationsTop 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applications
 
Rocket Fuelled Cucumbers
Rocket Fuelled CucumbersRocket Fuelled Cucumbers
Rocket Fuelled Cucumbers
 
O que tem de novo no Ruby 2.0?
O que tem de novo no Ruby 2.0?O que tem de novo no Ruby 2.0?
O que tem de novo no Ruby 2.0?
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5
 
First Day With J Ruby
First Day With J RubyFirst Day With J Ruby
First Day With J Ruby
 
Top 5 Mistakes When Writing Spark Applications
Top 5 Mistakes When Writing Spark ApplicationsTop 5 Mistakes When Writing Spark Applications
Top 5 Mistakes When Writing Spark Applications
 
Top 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applicationsTop 5 mistakes when writing Spark applications
Top 5 mistakes when writing Spark applications
 
Concurrency in ruby
Concurrency in rubyConcurrency in ruby
Concurrency in ruby
 
Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey
 
Tomcatx troubleshooting-production
Tomcatx troubleshooting-productionTomcatx troubleshooting-production
Tomcatx troubleshooting-production
 
(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task Execution(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task Execution
 
The Future of library dependency manageement of Ruby
The Future of library dependency manageement of RubyThe Future of library dependency manageement of Ruby
The Future of library dependency manageement of Ruby
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
 
TorqueBox for Rubyists
TorqueBox for RubyistsTorqueBox for Rubyists
TorqueBox for Rubyists
 
Memory Issues in Ruby on Rails Applications
Memory Issues in Ruby on Rails ApplicationsMemory Issues in Ruby on Rails Applications
Memory Issues in Ruby on Rails Applications
 
Matt Franklin - Apache Software (Geekfest)
Matt Franklin - Apache Software (Geekfest)Matt Franklin - Apache Software (Geekfest)
Matt Franklin - Apache Software (Geekfest)
 

Último

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
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 GoalsJhone kinadey
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
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..pdfPearlKirahMaeRagusta1
 
%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 Stilfonteinmasabamasaba
 
+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
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
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...SelfMade bd
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ
 
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 🔝✔️✔️Delhi Call girls
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
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.comFatema Valibhai
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Último (20)

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
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
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
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
 
%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
 
+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...
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
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...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
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 🔝✔️✔️
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
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
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Parallel Ruby: Managing the Memory Monster

  • 1. Parallel Ruby: Managing the Memory Monster Kevin Miller kevin.miller@flexport.com
  • 2.
  • 3. We run a lot of background jobs
  • 4. The Great Migration Old ● 240 Ruby Processes ● 20 Servers ● 12 Processes per Server ● 1 Thread per Process
  • 5. The Great Migration Old ● 240 Ruby Processes ● 20 Servers ● 12 Processes per Server ● 1 Thread per Process New ● 400 Ruby Threads ● 10 Servers ● 4 Processes per Server ● 10 Threads per Processes
  • 7. Side Lesson If you deploy every 10 minutes, you can get away with a lot
  • 8. And now back to business
  • 9.
  • 10.
  • 11. Ruby Heap: 40 byte slots
  • 12. Ruby Heap: 40 byte slots “String”
  • 13. Ruby Heap: 40 byte slots “String” Class.new
  • 14. Ruby Heap: 40 byte slots “String” Class.new {a: 2}
  • 15. Ruby Heap: 40 byte slots “String” Class.new {a: 2} [1]
  • 16. Ruby Heap: 40 byte slots “String” 12132425 Class.new {a: 2} [1]
  • 17. Ruby Heap: 40 byte slots “String” 12132425 Class.new 121325.224 {a: 2} [1]
  • 18. Ruby Heap: 40 byte slots “String” 12132425 Class.new 121325.224 {a: 2} /regular?/ [1]
  • 19. Ruby Heap: 40 byte slots “String” 12132425 Class.new 121325.224 {a: 2} /regular?/ [1] ...
  • 21. “String” 12132425 [“aaaaaaaaa... Class.new 121325.224 {a: 2} /regular?/ [1] ...
  • 22. “String” 12132425 [“aaaaaaaaa... Class.new 121325.224 {a: 2} /regular?/ [1] ...
  • 23. C (OS) Heap Pages: 4KB of space per page Big ArrayBig String
  • 24. “String” 12132425 ArraySlot Class.new 121325.224 {a: 2} /regular?/ [1] ... Big Array Big String [String, String...]
  • 25. “String” 12132425 ArraySlot “aaaa…” Class.new 121325.224 “aaaa…” “aaaa…” {a: 2} /regular?/ “aaaa…” “aaaa…” [1] ... “aaaa…” “aaaa…” Big Array Big String [String, String...]
  • 26. “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “String” 12132425 ArraySlot “aaaa…” Class.new 121325.224 “aaaa…” “aaaa…” {a: 2} /regular?/ “aaaa…” “aaaa…” [1] ... “aaaa…” “aaaa…” Big Array Big String [String, String...]
  • 27. “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “String” 12132425 ArraySlot StringSlot Class.new 121325.224 StringSlot StringSlot {a: 2} /regular?/ StringSlot StringSlot [1] ... StringSlot StringSlot Big Array Big String [String, String...]
  • 28. Big Array Big String [String, String...] “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “String” 12132425 ArraySlot StringSlot Class.new 121325.224 StringSlot StringSlot {a: 2} /regular?/ StringSlot StringSlot [1] ... StringSlot StringSlot Big Array Big String [String, String...]
  • 29. Total Slots = 1 for Array + 1024 for the 1024 strings = 1025 Total Ruby Heap Size = 1025 Slots * 40 bytes per slot = ~40KB = ~ .04 MB Total OS Heap Size = 4 bytes per array elem * 1024 array elems + 1024 bytes per string * 1024 strings = = 1028KB = ~1MB
  • 30.
  • 31. We expect memory to be around here
  • 32. Big Array Big String [String, String...] “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “String” 12132425 ArraySlot StringSlot Class.new 121325.224 StringSlot StringSlot {a: 2} /regular?/ StringSlot StringSlot [1] ... StringSlot StringSlot Big Array Big String [String, String...]
  • 33. Big Array Big String [String, String...] “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “aaaa…” “String” 12132425 ArraySlot StringSlot Class.new 121325.224 StringSlot StringSlot {a: 2} /regular?/ StringSlot StringSlot [1] ... StringSlot StringSlot Big Array Big String Wasted Space
  • 34. Ruby Threads Are Not Memory Friendly
  • 38. C Default: One OS Heap per Thread Thread 1 Thread 2 Fragmented OS Heap 1 Fragmented OS Heap 2
  • 39. C Default: One OS Heap per Thread Thread 1 Thread 2 Thread 20 ... Fragmented OS Heap 1 Fragmented OS Heap 2 Fragmented OS Heap 20
  • 40. Option 1: Reduce Number of OS Heaps Number of OS Heaps (a.k.a Arenas) can be configured with MALLOC_ARENA_MAX
  • 41. We expect memory to be around here
  • 42. Option 2: Use a Different Memory Allocator Can switch to jemalloc* over standard glibc malloc with LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 * After installing it via homebrew/apt-get/yum/etc
  • 43. We expect memory to be around here
  • 44. So… What should you do? Default Reduced Arena Count jemalloc Pros Out of the Box Works with all ruby Significant memory reduction Best Memory Usage Cons Heavy Fragmentation Increases contention in ruby implementations with true threading (JRuby) Requires reinstalling ruby and installing jemalloc Use Cases Single-threaded Ruby Programs on OS’s you don’t control Can set environment variables but cannot install packages Anytime you’re able to
  • 45. So… What should you do? 1) If you control the environment, jemalloc is king 2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2
  • 46. So… What should you do? 1) If you control the environment, jemalloc is king 2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2 “I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a miracle occurs: their memory usage drops massively” Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of standard Ruby
  • 47. So… What should you do? 1) If you control the environment, jemalloc is king 2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2 “I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a miracle occurs: their memory usage drops massively” “On Ubuntu Linux, with Rails Ruby Bench, jemalloc gives an overall speedup (not just memory, end-to-end speedup) of around 11%” Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of standard Ruby
  • 48. So… What should you do? 1) If you control the environment, jemalloc is king 2) If using MRI/CRuby, always set MALLOC_ARENA_MAX to 2 Taken from https://bugs.ruby-lang.org/issues/14718: proposal to make jemalloc part of standard Ruby “I supply a major piece of Ruby infrastructure (Sidekiq) and I keep hearing over and over how Ruby is terrible with memory, a huge memory hog with their Rails apps. My users switch to jemalloc and a miracle occurs: their memory usage drops massively” “On Ubuntu Linux, with Rails Ruby Bench, jemalloc gives an overall speedup (not just memory, end-to-end speedup) of around 11%” “I'm another Ruby user that used to have memory bloat problems and switched to jemalloc as well”
  • 49. Want to Learn More? Go to Aaron Patterson’s Talk: “Compacting Heaps in Ruby 2.7”