Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Garbage Collection em Ruby

Garbage Collection em Ruby

Palestra do TDC Florianópolis 2014

Lauro Caetano

May 17, 2014
Tweet

More Decks by Lauro Caetano

Other Decks in Programming

Transcript

  1. 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
  2. Varre novamente o heap, mas agora em busca dos objetos

    não marcados e os remove do heap
  3. ! 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! ! !
  4. 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
  5. 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
  6. 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
  7. Recuperar apenas o espaço necessário para criar um novo objeto

    e permitir que a aplicação continue rodando
  8. 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