SlideShare ist ein Scribd-Unternehmen logo
1 von 114
Downloaden Sie, um offline zu lesen
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
Open Source
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
Hora de limpar o Heap
Stop the world
Percorre todos os objetos
do heap e marca os que
ainda estão vivos
Varre novamente o heap, mas
agora em busca dos objetos não
marcados e os remove do heap
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
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 10 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

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Ruby GC Evolution