Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Server Memory - BuildStuff Ukraine 2019
Search
Андрей Листочкин (Andrey Listochkin)
November 18, 2019
Programming
0
26
Server Memory - BuildStuff Ukraine 2019
Андрей Листочкин (Andrey Listochkin)
November 18, 2019
Tweet
Share
More Decks by Андрей Листочкин (Andrey Listochkin)
See All by Андрей Листочкин (Andrey Listochkin)
Everybody Stand Back! I Know Regular Expressions
listochkin
0
140
Command-line scripting with Rust. Wait, what?!
listochkin
0
300
Server Memory - Chernivtsi JS 2019
listochkin
1
120
10 Years Later
listochkin
0
340
Managing Managers - DevTalks iHUB
listochkin
0
39
Time, Numbers, Text
listochkin
1
550
Software Licensing: A Minefield Guide
listochkin
0
130
We Make Bots. For Real
listochkin
0
390
Devops for A Busy Developer - XP Days 2016
listochkin
0
170
Other Decks in Programming
See All in Programming
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
270
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
930
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
290
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
9
2.3k
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
430
rails newと同時に型を書く
aki19035vc
5
700
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
VisionProで部屋の明るさを反映させるシェーダーを作った話
segur
0
100
Findy Team+ Awardを受賞したかった!ベストプラクティス応募内容をふりかえり、開発生産性向上もふりかえる / Findy Team Plus Award BestPractice and DPE Retrospective 2024
honyanya
0
140
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
1.1k
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
380
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
700
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
137
6.7k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2.1k
Navigating Team Friction
lara
183
15k
Being A Developer After 40
akosma
89
590k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
GraphQLとの向き合い方2022年版
quramy
44
13k
Site-Speed That Sticks
csswizardry
2
240
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
RailsConf 2023
tenderlove
29
970
Transcript
None
@listochkin + fb.com/tektonna
Memory Management
A bit deeper than most talks on similar subject
None
CPU Caches RAM Swap Disk Network
Operating System
None
Pages 4k
Virtual
3 processes [0 … ∞] [0 … ∞] [0 …
∞]
Allocate page But not really
None
malloc calloc realloc
None
pointer = malloc(size)
free(pointer)
malloc malloc malloc free malloc free free
Forget to free Free the wrong pointer Read from unallocated
memory
Can we do better?
Reference Counting
None
const players = [ … ] players.forEach(p => makeMove(p))
a = … doX(a) yield a
if ( … ) { let a = … …
}
C++: std::shared_ptr
None
Optimizations
Delayed Counting
Don’t count local references
function (user) { let email = user.email; … … }
Delayed deallocation
Reference Cycles
None
None
Compile time ref-counting
Several types of pointers
Ownership
Forbids cycles by forbidding several owning references at the same
time
None
None
None
Perl 1987 - today Python 1991 - 2001 PHP 1994
- 2009
Multiprocess deploys
fork
Copy on Write
Worker Accepts requests As memory use raises: Stop accepting requests
Complete in-flight requests Terminate
Master Keep track on workers Start new workers Signal them
to terminate when memory pressure is high
“Pre-fork”
Load the framework Load app code Run full GC ----
Start forking process to accept requests
None
None
“Processes” Supervision Trees
Tracing GC
1959 60 years ago
Start at Root references Follow all references Build a live
objects tree Delete all objects not part of the tree
Ruby Java JavaScript Lua Go Default Choice
Roots?
Constants Global variables Local variables Closures Thread-locals ...
Mark & Sweep vs Mark-Compact
None
Team Sweep: Go Ruby* Lua Embedded JS engines Erlang
Pros: Pointers don’t change Native extensions Easier to implement
Team Compact: Java JavaScript Ruby* Haskell
Pros: Less memory fragmentation over time Cons: Harder Takes longer
to do a GC
Incremental Marking
3-colored algorithm by Dijkstra™
None
None
None
None
None
What is a barrier? if statement
CPU branch predictor
Parallel marking
None
Lazy Sweep
Generational
Temporary data data.filter( … ).map( … ) log(`… ${data}`) JSON.stringify(result)
Major vs Minor GC
Pointers from Old objects to new objects
Remembered Set
Major GC Scan roots only Bigger object graph
Minor GC Scan roots + RS Small object graph
None
Modern GCs are hybrid
None
None
C-extensions
Can’t move objects if their references are passed to an
extension
Can’t add WB
RGen GC
2 types of objects WB-protected WB-unprotected
WBu are never OldGen
OldGen -> WBu WBu to Remembered Set
Mark WBus on every minor GC
Adding compaction for WBp
None
V8
Minor GC Parallel
Major Parallel marking Parallel / Concurrent Compact || Sweep
None
When to trigger GC?
Out-of-Bounds GC
Firefox Run GC in background tabs first instead of current
tab
Chrome Animation frame
OS Pages pre-forked processes malloc zones GC Regions Remembered Sets
Barriers
None
None
Practical Advice
Memory Leaks Multiprocess Deploys
Throughput ⇔ Latency
Throughput Batch jobs Queues
Latency Requests Messaging
Keep different types of workloads in different processes
More memory Swap with SSDs
Experiment and measure
@listochkin Hire Me!