SlideShare a Scribd company logo
Garbage Collection

Guido Wachsmuth




       Delft
                                    Course IN4303
       University of
       Technology             Compiler Construction
       Challenge the future
Recap
register allocation

How can we assign registers to local variables and temporaries?

  •   perform liveness analysis
  •   build interference graph
  •   colour interference graph

What to do if the graph is not colourable?

  •   keep local variables in memory

How to handle move instructions efficiently?

  •   coalesce nodes safely



                                                  Garbage Collection   2
Overview
today’s lecture

garbage collection




                     Garbage Collection   3
Overview
today’s lecture

garbage collection

mark & sweep

  •   mark reachable records
  •   sweep unmarked records




                               Garbage Collection   3
Overview
today’s lecture

garbage collection

mark & sweep

  •   mark reachable records
  •   sweep unmarked records

reference counting

  •   deallocate records with count 0




                                        Garbage Collection   3
Overview
today’s lecture

garbage collection

mark & sweep

  •   mark reachable records
  •   sweep unmarked records

reference counting

  •   deallocate records with count 0

copy collection

  •   copy reachable records
  •   consider generations of records

                                        Garbage Collection   3
I
garbage collection




                     Garbage Collection   4
the heap




           Garbage Collection   5
Recap: Java Virtual Machine
the heap

            method area                            stack

pc: 00              constant pool      optop: 00       local variables
00   12   ldc      00 4303 4303        00            00 002A 002A
01   00   00       01 0000 0004        01            01
02   19   aload    02                  02            02
03   00   00       03                  03            03
04   12   ldc      04                  04            04
05   01   01       05                  05            05
06   2E   iaload   06                  06            06

                                    heap

4303 4303 "Compilers"                002A 002A [20,01,40,02,42]

                                                       Garbage Collection   6
Recap: Java Virtual Machine
the heap

            method area                            stack

pc: 00
    02              constant pool      optop: 00
                                              01       local variables
00   12   ldc      00 4303 4303        00 4303 4303   00 002A 002A
01   00   00       01 0000 0004        01             01
02   19   aload    02                  02             02
03   00   00       03                  03             03
04   12   ldc      04                  04             04
05   01   01       05                  05             05
06   2E   iaload   06                  06             06

                                    heap

4303 4303 "Compilers"                002A 002A [20,01,40,02,42]

                                                       Garbage Collection   6
Recap: Java Virtual Machine
the heap

            method area                            stack

pc: 00
    04
    02              constant pool      optop: 00
                                              02
                                              01       local variables
00   12   ldc      00 4303 4303        00 4303 4303   00 002A 002A
01   00   00       01 0000 0004        01 002A 002A   01
02   19   aload    02                  02             02
03   00   00       03                  03             03
04   12   ldc      04                  04             04
05   01   01       05                  05             05
06   2E   iaload   06                  06             06

                                    heap

4303 4303 "Compilers"                002A 002A [20,01,40,02,42]

                                                       Garbage Collection   6
Recap: Java Virtual Machine
the heap

            method area                            stack

pc: 00
    06
    04
    02              constant pool      optop: 00
                                              03
                                              02
                                              01       local variables
00   12   ldc      00 4303 4303        00 4303 4303   00 002A 002A
01   00   00       01 0000 0004        01 002A 002A   01
02   19   aload    02                  02 0000 0004   02
03   00   00       03                  03             03
04   12   ldc      04                  04             04
05   01   01       05                  05             05
06   2E   iaload   06                  06             06

                                    heap

4303 4303 "Compilers"                002A 002A [20,01,40,02,42]

                                                       Garbage Collection   6
Recap: Java Virtual Machine
the heap

            method area                            stack

pc: 00
    07
    06
    04
    02              constant pool      optop: 00
                                              02
                                              01       local variables
00   12   ldc      00 4303 4303        00 4303 4303   00 002A 002A
01   00   00       01 0000 0004        01 002A 002A
                                          0000 0042   01
02   19   aload    02                  02 0000 0004   02
03   00   00       03                  03             03
04   12   ldc      04                  04             04
05   01   01       05                  05             05
06   2E   iaload   06                  06             06

                                    heap

4303 4303 "Compilers"                002A 002A [20,01,40,02,42]

                                                       Garbage Collection   6
Heap
  example




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9        20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection   7
II
mark & sweep




               Garbage Collection   8
Mark & sweep
idea

mark

  •     mark reachable records

  •     start at variables (roots)

  •     follow references

sweep

  •     marked records: unmark

  •     unmarked records: deallocate

  •     linked list of free records



                                       Garbage Collection   9
Marking
  example




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Marking
  example




12 ●   ●      15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                                 ● 37 ●
           roots
                                 p   q   r




                                                        Garbage Collection 10
Sweeping
  example




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Sweeping
  example




12 ●   ●     15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                            ●   ● 37 ●
  freelist
                            f   p   q   r




                                                       Garbage Collection 11
Marking
algorithms

function DFS(x)                function DFS(x)

  if pointer(x) & !marked[x]     if pointer(x) & !marked[x]

   marked[x] = true               marked[x] = true
                                  t = 1 ; stack[t] = x
    foreach f in fields(x)
                                   while t > 0
     DFS(f)
                                    x = stack[t] ; t = t - 1

                                     foreach f in fields(x)
                                       if pointer(f) & !marked[f]

                                        marked[f] = true
                                        t = t + 1 ; stack[t] = f




                                                 Garbage Collection 12
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Recap: Marking
  pointer reversals




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 13
Mark & sweep
notes

sweeping

  •   independent of marking algorithm

  •   several freelists (per record size)

  •   split free records for allocation

fragmentation

  •   external: many free records of small size

  •   internal: too-large record with unused memory inside




                                                        Garbage Collection 14
III
reference counts




                   Garbage Collection 15
Reference counts
idea

counts

  •      how many pointers point to each record?

  •      store with each record

counting

  •      extra instructions

deallocate

  •      put on freelist

  •      recursive deallocation on allocation


                                                   Garbage Collection 16
Reference counts
  example




12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 17
Reference counts
  example




12 1
   ●   ●   15 1
              ●   ●   ●17   37 2
                               ●    ●   59 1
                                           ●   ●    ●19           20 1
                                                                     ●     ●




                            ● 37 ●
                            p   q   r




                                                   Garbage Collection 17
Reference counts
  example




12 1
   ●   ●   15 1
              ●   ●   ●17   37 2
                               ●    ●   59 1
                                           ●   ●    ●19           20 1
                                                                     ●     ●




                            ● 37 ●
                            p   q   r




                                                   Garbage Collection 17
Reference counts
notes

cycles

  •      memory leaks

  •      break cycles explicitly

  •      occasional mark & sweep collection

expensive

  •      fetch, decrease, store old reference counter

  •      possible deallocation

  •      fetch, increase, store new reference counter



                                                        Garbage Collection 18
coffee break




               Garbage Collection 19
IV
copy collections




                   Garbage Collection 20
Copy collections
idea

spaces

  •      fromspace & tospace

  •      switch roles after copy

copy

  •      traverse reachability graph

  •      copy from fromspace to tospace

  •      fromspace unreachable, free memory

  •      tospace compact, no fragmentation



                                              Garbage Collection 21
Copy collection
  example


12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●   ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●    ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●
                               ●      ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●
                               ●      ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●

                                                     Garbage Collection 22
Copy collection
  example


12 ●   ●   15 ●
            ●     ●   ●   7   37 ●
                               ●      ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●

                                                     Garbage Collection 22
Copy collection

12 ●   ●   15 ●
            ●     ●   ●   7   37 ●
                               ●      ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●



                                                     Garbage Collection 23
Copy collection

12 ●   ●   15 ●
            ●     ●   ●   7   37 ●
                               ●      ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●



                                                     Garbage Collection 23
Copy collection

12 ●   ●   15 ●
            ●     ●   ●   7   37 ●
                               ●      ●   59 ●   ●    ●     9       20 ●     ●




                              ● 37 ●
                              p   q   r




15 ●   ●   37 ●   ●



                                                     Garbage Collection 23
Copy collection

12 ●   ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●     ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●    ●     9       20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●   ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection

12 ●
 ●     ●   15 ●
            ●     ●   ●   7       37 ●
                                   ●      ●   59 ●
                                               ●     ●       ●   9      20 ●
                                                                         ●       ●




                                  ● 37 ●
                                  p   q   r




15 ●   ●   37 ●   ●   12 ●    ●       20 ●    ●   59 ●   ●



                                                         Garbage Collection 23
Copy collection




                                 ● 37 ●
                                 p   q   r




15 ●   ●   37 ●   ●   12 ●   ●       20 ●    ●   59 ●   ●



                                                        Garbage Collection 23
Copy collections
algorithms

function Forward(p)            function BFS()

  if fromspace(p)               next = scan = start(tospace)
    if tospace(fields(p)[1])
      return fields(p)[1]        foreach r in root()
                                   r = Forward(r)
    else
                                 while scan < next
     foreach f in fields(p)
       next.f = f                  foreach f in fields(scan)
                                     f = Forward(f)
     fields(p)[1] = next
     next = next + size(p)        scan = scan + size(scan)
     return fields(p)[1]

  else return p




                                                 Garbage Collection 24
Copy collections
locality

adjacent records

  •   likely to be unrelated

pointers to records in records

  •   likely to be accessed

  •   likely to be far apart

solution

  •   depth-first copy: slow pointer reversals

  •   hybrid copy algorithm


                                                 Garbage Collection 25
Copy collections
generational collection

generations

   •   young data: likely to die soon

   •   old data: likely to survive for more collections

   •   divide heap, collect younger generations more frequently

collection

   •   roots: variables & pointers from older to younger generations

   •   preserve pointers to old generations

   •   promote objects to older generations



                                                          Garbage Collection 26
V
summary




          Garbage Collection 27
Summary
lessons learned

How can we collect unreachable records on the heap?

  •   reference counts
  •   mark reachable records, sweep unreachable records
  •   copy reachable records

How can we reduce heap space needed for garbage collection?

  •   pointer-reversal
  •   breadth-first search
  •   hybrid algorithms




                                                      Garbage Collection 28
Literature
learn more

Andrew W. Appel, Jens Palsberg: Modern Compiler
Implementation in Java, 2nd edition. 2002




                                                  Garbage Collection 29
Outlook
coming next

compiler components and their generators

  •   Lecture 11: Lexical Analysis
  •   Lecture 12: Syntactical Analysis
  •   Lecture 13: SDF inside
  •   Lecture 14: Static Analysis

Lab Nov 19 & Nov 26

  •   test cases
  •   finish Lab 2, do reference resolution next
  •   finish Lab 3, do hover help next
  •   content completion

                                                   Garbage Collection 30
questions




            Garbage Collection 31
credits




          Garbage Collection 32
Pictures
copyrights

Slide 1:
   Trash by Vladimer Shioshvili, some rights reserved

Slide 5:
   Gravel Pile by kenjonbro, some rights reserved

Slide 19:
   Typhoo by Dominica Williamson, some rights reserved

Slide 30:
   Romantic Pigeon Date by Harald Hoyer, some rights reserved

Slide 31:
   Questions by Oberazzi, some rights reserved

Slide 32:
   Too Much Credit by Andres Rueda, some rights reserved




                                                                Garbage Collection 33
Ad

More Related Content

More from Guido Wachsmuth (16)

Language
LanguageLanguage
Language
Guido Wachsmuth
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
Guido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
Guido Wachsmuth
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR Parsing
Guido Wachsmuth
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
Guido Wachsmuth
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Guido Wachsmuth
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation Records
Guido Wachsmuth
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Guido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
Guido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
Guido Wachsmuth
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
Guido Wachsmuth
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
Guido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
Guido Wachsmuth
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR Parsing
Guido Wachsmuth
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
Guido Wachsmuth
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Guido Wachsmuth
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
Guido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation Records
Guido Wachsmuth
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Guido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
Guido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
Guido Wachsmuth
 

Recently uploaded (20)

How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Ad

Compiling Imperative and Object-Oriented Languages - Garbage Collection

  • 1. Garbage Collection Guido Wachsmuth Delft Course IN4303 University of Technology Compiler Construction Challenge the future
  • 2. Recap register allocation How can we assign registers to local variables and temporaries? • perform liveness analysis • build interference graph • colour interference graph What to do if the graph is not colourable? • keep local variables in memory How to handle move instructions efficiently? • coalesce nodes safely Garbage Collection 2
  • 4. Overview today’s lecture garbage collection mark & sweep • mark reachable records • sweep unmarked records Garbage Collection 3
  • 5. Overview today’s lecture garbage collection mark & sweep • mark reachable records • sweep unmarked records reference counting • deallocate records with count 0 Garbage Collection 3
  • 6. Overview today’s lecture garbage collection mark & sweep • mark reachable records • sweep unmarked records reference counting • deallocate records with count 0 copy collection • copy reachable records • consider generations of records Garbage Collection 3
  • 7. I garbage collection Garbage Collection 4
  • 8. the heap Garbage Collection 5
  • 9. Recap: Java Virtual Machine the heap method area stack pc: 00 constant pool optop: 00 local variables 00 12 ldc 00 4303 4303 00 00 002A 002A 01 00 00 01 0000 0004 01 01 02 19 aload 02 02 02 03 00 00 03 03 03 04 12 ldc 04 04 04 05 01 01 05 05 05 06 2E iaload 06 06 06 heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
  • 10. Recap: Java Virtual Machine the heap method area stack pc: 00 02 constant pool optop: 00 01 local variables 00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A 01 00 00 01 0000 0004 01 01 02 19 aload 02 02 02 03 00 00 03 03 03 04 12 ldc 04 04 04 05 01 01 05 05 05 06 2E iaload 06 06 06 heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
  • 11. Recap: Java Virtual Machine the heap method area stack pc: 00 04 02 constant pool optop: 00 02 01 local variables 00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A 01 00 00 01 0000 0004 01 002A 002A 01 02 19 aload 02 02 02 03 00 00 03 03 03 04 12 ldc 04 04 04 05 01 01 05 05 05 06 2E iaload 06 06 06 heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
  • 12. Recap: Java Virtual Machine the heap method area stack pc: 00 06 04 02 constant pool optop: 00 03 02 01 local variables 00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A 01 00 00 01 0000 0004 01 002A 002A 01 02 19 aload 02 02 0000 0004 02 03 00 00 03 03 03 04 12 ldc 04 04 04 05 01 01 05 05 05 06 2E iaload 06 06 06 heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
  • 13. Recap: Java Virtual Machine the heap method area stack pc: 00 07 06 04 02 constant pool optop: 00 02 01 local variables 00 12 ldc 00 4303 4303 00 4303 4303 00 002A 002A 01 00 00 01 0000 0004 01 002A 002A 0000 0042 01 02 19 aload 02 02 0000 0004 02 03 00 00 03 03 03 04 12 ldc 04 04 04 05 01 01 05 05 05 06 2E iaload 06 06 06 heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] Garbage Collection 6
  • 14. Heap example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 7
  • 15. II mark & sweep Garbage Collection 8
  • 16. Mark & sweep idea mark • mark reachable records • start at variables (roots) • follow references sweep • marked records: unmark • unmarked records: deallocate • linked list of free records Garbage Collection 9
  • 17. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 10
  • 18. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 19. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 20. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 21. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 22. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 23. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 24. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 25. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 26. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 27. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 28. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 29. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 30. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 31. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 32. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 33. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 34. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 35. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 36. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 37. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 38. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 39. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 40. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 41. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 42. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 43. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 44. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 45. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 46. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 47. Marking example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● roots p q r Garbage Collection 10
  • 48. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 11
  • 49. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 50. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 51. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 52. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 53. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 54. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 55. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 56. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 57. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 58. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 59. Sweeping example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● freelist f p q r Garbage Collection 11
  • 60. Marking algorithms function DFS(x) function DFS(x) if pointer(x) & !marked[x] if pointer(x) & !marked[x] marked[x] = true marked[x] = true t = 1 ; stack[t] = x foreach f in fields(x) while t > 0 DFS(f) x = stack[t] ; t = t - 1 foreach f in fields(x) if pointer(f) & !marked[f] marked[f] = true t = t + 1 ; stack[t] = f Garbage Collection 12
  • 61. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 62. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 63. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 64. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 65. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 66. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 67. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 68. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 69. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 70. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 71. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 72. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 73. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 74. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 75. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 76. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 77. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 78. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 79. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 80. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 81. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 82. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 83. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 84. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 85. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 86. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 87. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 88. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 89. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 90. Recap: Marking pointer reversals 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 13
  • 91. Mark & sweep notes sweeping • independent of marking algorithm • several freelists (per record size) • split free records for allocation fragmentation • external: many free records of small size • internal: too-large record with unused memory inside Garbage Collection 14
  • 92. III reference counts Garbage Collection 15
  • 93. Reference counts idea counts • how many pointers point to each record? • store with each record counting • extra instructions deallocate • put on freelist • recursive deallocation on allocation Garbage Collection 16
  • 94. Reference counts example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 17
  • 95. Reference counts example 12 1 ● ● 15 1 ● ● ●17 37 2 ● ● 59 1 ● ● ●19 20 1 ● ● ● 37 ● p q r Garbage Collection 17
  • 96. Reference counts example 12 1 ● ● 15 1 ● ● ●17 37 2 ● ● 59 1 ● ● ●19 20 1 ● ● ● 37 ● p q r Garbage Collection 17
  • 97. Reference counts notes cycles • memory leaks • break cycles explicitly • occasional mark & sweep collection expensive • fetch, decrease, store old reference counter • possible deallocation • fetch, increase, store new reference counter Garbage Collection 18
  • 98. coffee break Garbage Collection 19
  • 99. IV copy collections Garbage Collection 20
  • 100. Copy collections idea spaces • fromspace & tospace • switch roles after copy copy • traverse reachability graph • copy from fromspace to tospace • fromspace unreachable, free memory • tospace compact, no fragmentation Garbage Collection 21
  • 101. Copy collection example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 22
  • 102. Copy collection example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r Garbage Collection 22
  • 103. Copy collection example 12 ● ● 15 ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● Garbage Collection 22
  • 104. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● Garbage Collection 22
  • 105. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● Garbage Collection 22
  • 106. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● Garbage Collection 22
  • 107. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 22
  • 108. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 22
  • 109. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 22
  • 110. Copy collection example 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 22
  • 111. Copy collection 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 23
  • 112. Copy collection 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 23
  • 113. Copy collection 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● Garbage Collection 23
  • 114. Copy collection 12 ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 115. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 116. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 117. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 118. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 119. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 120. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● Garbage Collection 23
  • 121. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
  • 122. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
  • 123. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
  • 124. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● Garbage Collection 23
  • 125. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 126. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 127. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 128. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 129. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 130. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 131. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 132. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 133. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 134. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 135. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 136. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 137. Copy collection 12 ● ● ● 15 ● ● ● ● 7 37 ● ● ● 59 ● ● ● ● 9 20 ● ● ● ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 138. Copy collection ● 37 ● p q r 15 ● ● 37 ● ● 12 ● ● 20 ● ● 59 ● ● Garbage Collection 23
  • 139. Copy collections algorithms function Forward(p) function BFS() if fromspace(p) next = scan = start(tospace) if tospace(fields(p)[1]) return fields(p)[1] foreach r in root() r = Forward(r) else while scan < next foreach f in fields(p) next.f = f foreach f in fields(scan) f = Forward(f) fields(p)[1] = next next = next + size(p) scan = scan + size(scan) return fields(p)[1] else return p Garbage Collection 24
  • 140. Copy collections locality adjacent records • likely to be unrelated pointers to records in records • likely to be accessed • likely to be far apart solution • depth-first copy: slow pointer reversals • hybrid copy algorithm Garbage Collection 25
  • 141. Copy collections generational collection generations • young data: likely to die soon • old data: likely to survive for more collections • divide heap, collect younger generations more frequently collection • roots: variables & pointers from older to younger generations • preserve pointers to old generations • promote objects to older generations Garbage Collection 26
  • 142. V summary Garbage Collection 27
  • 143. Summary lessons learned How can we collect unreachable records on the heap? • reference counts • mark reachable records, sweep unreachable records • copy reachable records How can we reduce heap space needed for garbage collection? • pointer-reversal • breadth-first search • hybrid algorithms Garbage Collection 28
  • 144. Literature learn more Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Garbage Collection 29
  • 145. Outlook coming next compiler components and their generators • Lecture 11: Lexical Analysis • Lecture 12: Syntactical Analysis • Lecture 13: SDF inside • Lecture 14: Static Analysis Lab Nov 19 & Nov 26 • test cases • finish Lab 2, do reference resolution next • finish Lab 3, do hover help next • content completion Garbage Collection 30
  • 146. questions Garbage Collection 31
  • 147. credits Garbage Collection 32
  • 148. Pictures copyrights Slide 1: Trash by Vladimer Shioshvili, some rights reserved Slide 5: Gravel Pile by kenjonbro, some rights reserved Slide 19: Typhoo by Dominica Williamson, some rights reserved Slide 30: Romantic Pigeon Date by Harald Hoyer, some rights reserved Slide 31: Questions by Oberazzi, some rights reserved Slide 32: Too Much Credit by Andres Rueda, some rights reserved Garbage Collection 33

Editor's Notes