Slide 1

Slide 1 text

Incremental  Mature  Garbage   Collec2on  Using  the  Train  Algorithm   A  totally  awesome  paper!  

Slide 2

Slide 2 text

About  Me   Andrew  Turley   [email protected]   @casio_juarez   Junior  SoEware  Engineer  at  TheLadders     (we’re  hiring!)  

Slide 3

Slide 3 text

About  The  Paper   Incremental  Mature  Garbage  Collec2on  Using   the  Train  Algorithm   Jacob  Seligmann  and  Steffen  Grarup     Presented  at  the  Ninth  European  Conference  on   Object-­‐Oriented  Programming  in  1995  

Slide 4

Slide 4 text

Overview   Incremental  Mature  Garbage  Collec2on  Using   the  Train  Algorithm   •  Garbage  Collec2on   •  Mature   •  Incremental   •  The  Train  Algorithm  

Slide 5

Slide 5 text

Incremental  Mature  Garbage   Collec2on  Using  the  Train  Algorithm  

Slide 6

Slide 6 text

Garbage  Collec2on  -­‐  What  Is  Garbage?   // a has a member p! a = new ExcellentClass();! ! // create an object and assign it to a.p! a.p = new VeryExcellentClass();! ! // create another object and assign it to a.p! a.p = new VeryExcellentClass();! ! !

Slide 7

Slide 7 text

Garbage  Collec2on  –  What  Is   Collec2on?   •  Make  memory  used  by  dead  objects  available   for  exis2ng  or  new  objects  

Slide 8

Slide 8 text

Garbage  Collec2on  -­‐  Manual  vs   Automa2c   •  Manual  –  the  programmer  explicitly  tells  the   system  when  an  object  is  no  longer  being  used   •  Automa2c  –  the  system  automa2cally   determines  when  an  object  is  no  longer  being   used   – “no  longer  being  used”  ==  unreachable  from  roots  

Slide 9

Slide 9 text

Garbage  Collec2on  –  Why  Bother?   •  Might  eventually  run  out  of  memory   •  Put  related  objects  closer  together  in  memory  

Slide 10

Slide 10 text

Garbage  Collec2on  -­‐  Two  Main  Types   •  Coun2ng  –  keep  track  of  how  many  other   objects  reference  an  object,  if  the  number  of   references  reaches  0  then  collect  the  object   •  Tracing  –  when  you  run  out  of  memory,  find   all  the  reachable  objects,  collect  all  the  other   (unreachable)  objects  

Slide 11

Slide 11 text

Garbage  Collec2on  -­‐  Coun2ng  

Slide 12

Slide 12 text

Garbage  Collec2on  -­‐  Tracing  

Slide 13

Slide 13 text

Garbage  Collec2on  -­‐  When  Do  You   Collect?   Generally  (naively)  …   •  Coun2ng  -­‐-­‐  Collect  as  soon  as  an  object   becomes  unreachable  (count  =  0)   •  Tracing  -­‐-­‐  Collect  when  there's  no  more   memory  leE  

Slide 14

Slide 14 text

Garbage  Collec2on  -­‐  Compac2ng   •  Once  you  get  rid  of  dead  objects,  move  live   objects  closer  together   •  Can  improve  locality   •  There’s  a  cost  

Slide 15

Slide 15 text

Garbage  Collec2on  -­‐  Considera2ons   •  Throughput  -­‐-­‐  2me  outside  of  gc  /  total   running  2me   •  Pause  Times  -­‐-­‐  2me  the  applica2on  is  paused   for  collec2on   •  Memory  Overhead  -­‐-­‐  memory  used  to  track   objects  for  garbage  collec2on  

Slide 16

Slide 16 text

Incremental  Mature  Garbage   Collec2on  Using  the  Train  Algorithm  

Slide 17

Slide 17 text

Mature  -­‐  The  Weak  Genera2onal   Hypothesis   •  “Most  objects  die  young.”  

Slide 18

Slide 18 text

Mature  -­‐  Separate  Genera2ons  of   Objects   •  Allocate  objects  in  one  part  of  the  heap,   promote  them  to  another  part  if  they  live  long   enough  

Slide 19

Slide 19 text

Mature  -­‐  Don't  Need  Collect  All   Genera2ons  At  Once   •  By  keeping  the  younger  genera2on  smaller,   there  are  fewer  objects  to  trace,  so  collec2ng   this  genera2on  is  fast  

Slide 20

Slide 20 text

Mature  -­‐  Different  Algorithms  For   Different  Genera2ons   •  Younger  genera2ons  are  small  and  unstable   •  Older  genera2ons  are  larger  and  stable   •  Make  different  2me  and  space  tradeoffs  

Slide 21

Slide 21 text

Mature  -­‐  Remembered  Sets   •  Record  references  between  genera2ons,  use   old-­‐to-­‐young  as  roots  when  tracing   •  Collector  doesn’t  need  to  scan  Old  when   collec2ng  Young  

Slide 22

Slide 22 text

Mature  -­‐  You  S2ll  Pay  A  Price   •  If  the  old  genera2on  is  large,  collec2ng  it  can   take  ...  a  very  long  2me  

Slide 23

Slide 23 text

Incremental  Mature  Garbage   Collec2on  Using  the  Train  Algorithm  

Slide 24

Slide 24 text

Incremental  -­‐  Don’t  Do  Everything  At   Once   •  Only  collect  a  subset  of  the  heap  at  a  2me   •  Reduces  pause  2mes  

Slide 25

Slide 25 text

Incremental  -­‐  Picking  Subsets   •  Collect  related  objects  at  the  same  2me   (Baker,  1977)   •  How  do  we  figure  out  which  objects  are   "related"?  

Slide 26

Slide 26 text

Incremental  Mature  Garbage   Collec2on  Using  the  Train  Algorithm  

Slide 27

Slide 27 text

Train  Algorithm  -­‐  Originally   Incremental  Collec2on  of  Mature  Objects   Richard  Hudson  and  Eliot  Moss  

Slide 28

Slide 28 text

Train  Algorithm  -­‐  A  Real   Implementa2on   Jacob  Seligmann  and  Steffen  Grarup   Goal:  non-­‐disrup2ve  GC  for  interac2ve  systems  

Slide 29

Slide 29 text

Train  Algorithm  -­‐  Target  Plalorm   •  Mjølner  (me-­‐'ol-­‐near)  BETA  System   •  Basically  an  IDE  wrinen  in  and  suppor2ng  the   BETA  programming  language   •  Sun  SPARC  IPX  4/50,  40  Mhz,  32MB  RAM  

Slide 30

Slide 30 text

Train  Algorithm  -­‐  Structure  Of  The   Heap   •  Young  Genera2on  (Infant  Object  Area)  -­‐-­‐  semi-­‐ space  copying  collector   •  Old  Genera2on  (Mature  Object  Area)  -­‐-­‐  the   Train  Algorithm  (previously  used  mark-­‐sweep)   •  Large  Value  Area  -­‐-­‐  free  lists,  periodic   compac2on  

Slide 31

Slide 31 text

Train  Algorithm  -­‐  Structure  Of  The   Mature  Space   •  Collec2ons  of  fixed-­‐size  areas  into  which   objects  are  copied   •  fixed-­‐size  area  =  car   •  collec2on  of  cars  =  train   •  Cars  are  ordered  in  trains,  trains  are  ordered   by  crea2on  2me    

Slide 32

Slide 32 text

The  Train  Algorithm   SEE  THE  HANDOUT  

Slide 33

Slide 33 text

Train  Algorithm  -­‐  Tenuring  Strategy   "Objects  promoted  from  younger  genera2ons   may  be  stored  in  any  train  except  the  one   currently  being  collected,  or  one  or  more  new   trains  may  be  created  to  hold  them."  

Slide 34

Slide 34 text

Train  Algorithm  -­‐  Evacua2on  Strategy   •  How  are  live  objects  moved  out  of  the  car   being  collected?   •  Evacuate  to  the  last  car  of  the  first  train  seen  

Slide 35

Slide 35 text

Train  Algorithm  –  Example  1  

Slide 36

Slide 36 text

Train  Algorithm  –  Example  2  

Slide 37

Slide 37 text

Train  Algorithm  –  Example  3  

Slide 38

Slide 38 text

Train  Algorithm  –  Example  4  

Slide 39

Slide 39 text

Train  Algorithm  –  Example  5  

Slide 40

Slide 40 text

Train  Algorithm  –  Example  6  

Slide 41

Slide 41 text

Train  Algorithm  -­‐  Fu2le  Collec2ons   •  A  “fu2le  collec2on”  is  a  collec2on  that  does   not  result  in  an  evacua2on,  nor  does  it  reclaim   objects  

Slide 42

Slide 42 text

Train  Algorithm  -­‐  Why  It  Works   •  Groups  of  connected  objects  end  up  in  the   same  train   •  Live  objects  are  moved  to  later  trains,  dead   objects  stay  put  and  eventually  their  train  is   collected   •  “It’s  like  the  train  is  headed  for  a  cliff,  and  live   objects  get  pulled  into  other  cars  or  other   trains  by  their  friends.”  –  Franco  Barbeite  

Slide 43

Slide 43 text

Train  Algorithm  -­‐  Remembered  Sets   •  Cars  have  remembered  sets  that  contain   informa2on  about  all  references  residing   outside  the  car  poin2ng  into  it   – Only  need  to  remember  reference  from  higher   numbered  to  lower  numbered  cars   •  Cars  have  remembered  sets  that  contain   references  to  the  young  genera2on  

Slide 44

Slide 44 text

Train  Algorithm  -­‐  Invoca2on  Frequency   •  Trade  off  between  keeping  the  mature  space   rela2vely  garbage-­‐free  and  not  was2ng  too   much  2me  moving  live  objects   •  "Acceptable  Garbage  Percentage"  and  upper   limit  on  number  of  young  genera2on   collec2ons  between  old  genera2on  collec2ons   •  Garbage  Ra2o  is  es2mated  aEer  each  train   collec2on  by  keeping  track  of  the  size  of  data   directly  tenured  in  each  train  (see  thesis)  

Slide 45

Slide 45 text

Train  Algorithm  -­‐  Popular  Objects   •  All  Mjølner  BETA  programs  have  an   environment  object  which  is  referenced  by   lots  of  other  objects   •  Store  this  object  in  a  special  car  with  no   remembered  set,  when  this  car  comes  up  for   collec2on  move  it  to  a  new  train   •  Chea2ng!  (not  really  general  purpose)  

Slide 46

Slide 46 text

Train  Algorithm  -­‐  Results   •  We  won!   •  Reduced  max  and  average  pause  2mes  (3.21s  vs  0.12s  and   1.71s  vs  0.04s  in  the  editor)   •  Minimal  impact  on  GC  overhead  (pointer  book  keeping,   etc)   •  20%  overall  increase  in  old  genera2on  collec2on  2me   •  Small  increase  in  storage  requirements  (larger   remembered  sets,  train  accoun2ng)   •  Slight  increase  in  garbage  overhead  (uncollected  garbage)   •  Popular  object  treatment  was  a  really  good  idea  (30%   worse  without)   •  Improved  locality  

Slide 47

Slide 47 text

Train  Algorithm  -­‐  And  Then  What?   •  Was  added  to  version  1.3.1  of  the  Hotspot   JVM  in  1998   – Lots  of  work  by  Alex  Garthwaite   •  Was  later  deprecated  because  of  issues  with   cycle  detec2on  and  high  overhead  with   respect  to  throughput   •  The  ideas  live  on  in  the  G1  (garbage  first)   collector  

Slide 48

Slide 48 text

Why  I  Love  This  Paper   •  Trains!   •  Engineering  over  theory   •  The  Train  Algorithm  is  a  good  illustra2on  of   how  fiendishly  difficult  garbage  collec2on  can   be  

Slide 49

Slide 49 text

  WELL   ACTUALLY