Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Server Memory - Chernivtsi JS 2019
Андрей Листочкин (Andrey Listochkin)
June 22, 2019
Programming
1
31
Server Memory - Chernivtsi JS 2019
Андрей Листочкин (Andrey Listochkin)
June 22, 2019
Tweet
Share
More Decks by Андрей Листочкин (Andrey Listochkin)
See All by Андрей Листочкин (Andrey Listochkin)
Everybody Stand Back! I Know Regular Expressions
listochkin
0
35
Command-line scripting with Rust. Wait, what?!
listochkin
0
140
Server Memory - BuildStuff Ukraine 2019
listochkin
0
12
10 Years Later
listochkin
0
230
Managing Managers - DevTalks iHUB
listochkin
0
28
Time, Numbers, Text
listochkin
1
250
Software Licensing: A Minefield Guide
listochkin
0
82
We Make Bots. For Real
listochkin
0
230
Devops for A Busy Developer - XP Days 2016
listochkin
0
97
Other Decks in Programming
See All in Programming
WindowsコンテナDojo : 第1回 Visual StudioでWindowsコンテナアプリ作成
oniak3ibm
PRO
0
320
Node.js 最新動向 TFCon 2022
yosuke_furukawa
PRO
6
2.7k
TechFeed Conference 2022 - Kotlin Experimental
jmatsu
0
620
UI State Modeling 어떤게 좋을까?
laco2951
1
220
Managing gRPC with Wire
oldergod
2
150
機能横断型チームにおける技術改善
takeshiakutsu
3
440
2022 Android Training
mixi_engineers
1
660
확장 가능한 테라폼 코드 관리 (Scalable Terraform Code Management)
posquit0
1
310
実録mruby組み込み体験
coe401_
0
100
Becoming an Android Librarian
skydoves
3
440
Learning DDD輪読会#4 / Learning DDD Book Club #4
suzushin54
1
110
よりUXに近いSLI・SLOの運用による可用性の再設計
kazumanagano
3
480
Featured
See All Featured
It's Worth the Effort
3n
172
25k
Writing Fast Ruby
sferik
612
57k
The Art of Programming - Codeland 2020
erikaheidi
31
5.8k
GraphQLとの向き合い方2022年版
quramy
16
8.1k
Atom: Resistance is Futile
akmur
255
20k
Bootstrapping a Software Product
garrettdimon
295
110k
The Language of Interfaces
destraynor
148
20k
The Mythical Team-Month
searls
208
39k
Support Driven Design
roundedbygravity
86
8.4k
Embracing the Ebb and Flow
colly
73
3.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
225
120k
The Cult of Friendly URLs
andyhume
68
4.7k
Transcript
None
@listochkin fb.com/tektonna
Not a JavaScript talk
No CSS No frameworks No Node
A bit deeper than most talks on similar subject
None
How programming languages work
Just-in-time compilers Garbage Collection
Memory Management
Server runs our programs
CPU Caches RAM Swap Disk Network
Operating System
None
Pages
4k
Virtual
2 processes [0 … ∞]
Oh, I got a pointer at 4096 What’s at 4095?
Page fault
Allocate page But not really
Once you start writing to it
not 4k?
None
None
malloc
None
malloc me some memory
a pointer
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
scope
if ( … ) { let a = … …
}
None
C++: std::shared_ptr
None
Optimizations
Delayed Counting
Don’t count local references
function (user) { let email = user.email; … … }
Delayed deallocation
Cycle Collector
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”
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
Roots?
Constants Global variables Local variables Closures Thread-locals ...
Mark & Sweep vs Mark-Compact
None
Can you move objects after mark?
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
Mark all memory
None
Incremental Marking
3-colored algorithm by Dijkstra™
None
None
None
None
None
What is a barrier? if statement
else branch is very rare CPU branch predictor
Parallel marking
None
None
Lazy Sweep
Generational
Temporary data
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
1 stw to mark all WBu in RS
None
None
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
request? Minor GCs response is sent? Major GC
Firefox Run GC in background tabs first instead of current
tab
Chrome Animation frame
Walking the memory
Cache locality
GPU
OS Pages pre-forked processes malloc zones GC Pages Remembered Sets
Barriers
None
None