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
Garbage Collection em Ruby
Search
Lauro Caetano
May 17, 2014
Programming
2
120
Garbage Collection em Ruby
Palestra do TDC Florianópolis 2014
Lauro Caetano
May 17, 2014
Tweet
Share
More Decks by Lauro Caetano
See All by Lauro Caetano
Learning Elixir by Examples
laurocaetano
1
210
What's new in Rails 4.1
laurocaetano
0
84
Clojure Introduction.
laurocaetano
1
67
Other Decks in Programming
See All in Programming
How to Break into Reading Open Source
kaspth
2
210
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
800
GenU導入でCDKに初挑戦し、悪戦苦闘した話
hideg
0
160
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
260
Ruby Parser progress report 2024
yui_knk
2
230
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
210
Modular Monolith Go Server with GraphQL Federation + gRPC
110y
1
580
Crafting Cross-Platform Adventures: Building a Game Engine with Kotlin Multiplatform
dwursteisen
0
120
私のEbitengineの第一歩
qt_luigi
0
450
長期運用プロダクトの開発速度を維持し続けるためのリファクタリング実践例
wataruss
8
2.7k
状態管理ライブラリZustandの導入から運用まで
k1tikurisu
3
470
Go Code Generation at newmo / 2024-08-27 #newmo_layerx_go
genkey6
0
560
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
64
11k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
45
4.8k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
26
1.9k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
The Invisible Customer
myddelton
119
13k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
38
9.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.1k
Navigating Team Friction
lara
183
13k
GitHub's CSS Performance
jonrohan
1030
450k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
23
1.7k
KATA
mclloyd
27
13k
Transcript
Garbage Collection Ruby
Gerenciamento manual de memória
Objeto 01 Objeto 02
Desalocar manualmente
Objeto 01 Objeto 02
Objeto 01
Mas imagine o seguinte cenário
Objeto 01 Objeto 02
Objeto 01 Objeto 02
Referência para um objeto que não existe mais
Seu sistema pode quebrar
Ou pior…
Ele pode gerar resultados incorretos
Outras preocupações além do domínio da aplicação
Entender como os módulos que você utiliza gerenciam sua memória
Lauro Caetano @laurocaetano
None
None
Open Source
None
None
None
Rails
Rails core team
O que é Garbage Collection e como ele foi evoluindo
a cada nova versão do Ruby
Agenda • O que é Garbage Collection • Garbage Collection
no Ruby - Mark and sweep • Desvantagens do Mark and Sweep • Evolução • Lazy Sweep - Ruby 1.9 • Bitmap Mark - Ruby 2.0 • Generational Garbage Collection - Ruby 2.1
O que é Garbage Collection?
Gerenciamento dinâmico automático de memória
3 Responsabilidades
1. Alocar espaço para novos objetos
2. Identificar os objetos ainda vivos
3. Recuperar o espaço ocupado por objetos mortos
Garbage Collection no Ruby
Ruby ainda utiliza o mesmo algoritmo que foi inventado a
mais de 50 anos por John MacCarthy
Mark-Sweep
Free List
None
None
None
None
None
Hora de limpar o Heap
Stop the world
Percorre todos os objetos do heap e marca os que
ainda estão vivos
None
None
None
None
None
Varre novamente o heap, mas agora em busca dos objetos
não marcados e os remove do heap
None
None
None
None
None
None
None
None
Volta a executar o seu programa
Vamos ao código
! 1 def new! 2 ref = allocate! 3 !
4 if ref.nil?! 5 collect! 6 ! 7 if ref.nil?! 8 raise "Out of memory"! 9 end! 10 end! 11 end! ! !
12 ! 13 def collect! 14 mark_from_roots! 15 sweep(@heap_start, @heap_end)!
16 end! 17 ! ! !
17 ! 18 def mark_from_roots! 19 initialize(worklist)! 20 ! 21
@roots.each do |fld|! 22 ref = fld! 23 if !ref.nil? && !marked?(ref)! 24 set_marked(ref)! 25 mark! 26 end! 27 end! 28 end! 29
32 ! 33 def mark! 34 while(@worklist.any?)! 35 ref =
remove(worklist)! 36 ! 37 pointers(ref).each do |fld|! 38 child = fld! 39 ! 40 if !child.nil? && !marked?(child)! 41 set_marked(child)! 42 add(@worklist, child)! 43 end! 44 end! 45 end! 46 end! 47
47 ! 48 def sweep(heap_start, heap_end)! 49 scan = start!
50 ! 51 while (scan < heap_end)! 52 if marked?(scan)! 53 unset_marked(scan)! 54 else! 55 free(scan)! 56 end! 57 ! 58 scan = next_object(scan)! 59 end! 60 end
Desvantagens do Mark and sweep
Fazer Full GC toda hora que o heap está cheio
não parece uma boa ideia
Stop the world
Mark and sweep é proporcional ao tamanho do heap
Evolução
Lazy Sweep Ruby 1.9
Fazer Full GC toda hora que o heap está cheio
não parece uma boa ideia
Recuperar apenas o espaço necessário para criar um novo objeto
e permitir que a aplicação continue rodando
Agora o sweep não para mais o mundo todo
A alocação é responsável por checar se existe espaço disponível
Caso não tenha espaço disponível, fazer Lazy Sweep até obter
o espaço necessário
12 ! 13 def allocate(size)! 14 result = remove(size)! 15
! 16 if result.nil?! 17 lazy_sweep(size)! 18 result = remove(size)! 19 end! 20 ! 21 result! 22 end! 23
Bitmap Mark Ruby 2.0
None
Como os bits de marcação no ficavam objeto, os valores
eram sempre diferentes
Para possibilitar que o Unix compartilhe valores iguais
Os bits de marcação foram movidos para uma tabela de
bitmap
1 1 1 1 1 0 0
Generational Garbage Collection Ruby 2.1
Mark and sweep é proporcional ao tamanho do heap
Weak generational hypothesis
O coletor processa mais frequentemente objetos novos do que objetos
maduros
Um objeto maduro é aquele que permanece ativo por um
algum período de tempo
Enquanto um objeto novo é aquele que seu sistema acabou
de criar
Geração Nova Geração Madura
Quando o mark and sweep terminar, os objetos restantes serão
considerados maduros
Geração Nova Geração Madura
Geração Nova Geração Madura
Objetos maduros não serão marcados novamente até um próximo full
GC
Quando o full GC ocorrer, todos os objetos passarão pelo
mark and sweep
É legal entender como as coisas funcionam
Obrigado @laurocaetano github.com/laurocaetano
Bonus
JRuby and Rubinius.
Copying Garbage Collection
Segmento de memória contínua
Bump Allocation
Aloca segmentos adjacentes de memória, mantendo um ponteiro para a
próxima alocação
Próxima alocação
Próxima alocação
Próxima alocação
Objetos podem ser de tamanhos diferentes
A grande vantagem é que valores relacionados ficam próximos
A CPU pode cachear a região de memória que for
acessada frequentemente
Também utiliza o Garbage Collection Generacional
Ruby 2.2
GC Simbolos
https://bugs.ruby-lang.org/issues/9634
Obrigado @laurocaetano github.com/laurocaetano