Slide 1

Slide 1 text

THE GOLANG GARBAGE COLLECTOR Maya Rosecrance

Slide 2

Slide 2 text

MEMORY MANAGEMENT SYSTEM ● Memory Allocation ● Marking of live objects ● Freeing space used by dead objects

Slide 3

Slide 3 text

CONTENTS ● Garbage generation ● Garbage Collection ● Measuring performance ● Configuring the Garbage Collector

Slide 4

Slide 4 text

WHEN IS GARBAGE GENERATED func main() { NewDuck() } type Duck struct {} func NewDuck() Duck { return Duck{} } Stack Main() Duck NewDuck()

Slide 5

Slide 5 text

WHEN IS GARBAGE GENERATED func main() { NewDuck() } type Duck struct {} //go:noinline func NewDuck() *Duck { return &Duck{} } Stack & Heap Duck

Slide 6

Slide 6 text

WHEN IS GARBAGE GENERATED ● go run -gcflags -m main.go # command-line-arguments ./main.go:16:6: cannot inline NewDuck: marked go:noinline ./main.go:9:6: cannot inline main: non-leaf function ./main.go:17:15: &Duck literal escapes to heap ./main.go:17:15: from ~r0 (return) at ./main.go:17:2

Slide 7

Slide 7 text

WHEN IS GARBAGE GENERATED func main() { var a Bird a = NewDuck() a.Tweet() } type Bird interface { Tweet() } Stack a Heap Duck Bird

Slide 8

Slide 8 text

TRIGGER THE GARBAGE COLLECTOR Duck Goose 50mb Trigger at 100mb

Slide 9

Slide 9 text

GC PACER Core Core OS Tread OS Tread G o G o G o G o G o G o G o G o

Slide 10

Slide 10 text

CONCURRENT, TRICOLOR MARK AND SWEEP *NON-GENERATIONAL, NON-MOVING

Slide 11

Slide 11 text

CONCURRENT MARK AND SWEEP Stack Heap Duck Goose Gosling Duck Goose Gosling Goose Goose Gosling Gosling MooSe Stop https://go.googlesource.com/proposal/+/master/design/17503-eliminate-res can.md main() MooseMaker( ) a a NewMoo se() B Stop

Slide 12

Slide 12 text

TIMELINE Mark and Sweep Tricolor, Concurrent Mark and Sweep Weak Invariant Tricolor Mark and Sweep 1.5 1.9 1.0

Slide 13

Slide 13 text

TRADEOFFS ● GC throughput ● Program throughput ● Pause times (Latency)

Slide 14

Slide 14 text

MEASUREMENT Performance

Slide 15

Slide 15 text

BENCHMARKS ● Be wary of toy Garbage Collection tests ● Don’t behave the same a “real” programs ● locality effects ● Optimum dataset for any algorithm

Slide 16

Slide 16 text

TOOLS ● pprof ● gctrace=1 ● sync.Pool ● https://github.com/golang/go/wiki/Performance ● https://golang.org/pkg/runtime/ ● https://www.ardanlabs.com/blog/2017/06/language-mechanics-on-memory-profiling.h tml

Slide 17

Slide 17 text

TIME Stack Allocation Alone - 10 ms Original - 300 ms

Slide 18

Slide 18 text

TIME GC STW- 100 microseconds Humans process visual stimuli - 13 ms Speed of light from Tel Aviv to San Francisco - 40ms

Slide 19

Slide 19 text

GARBAGE COLLECTOR KNOBS Tweak the GC

Slide 20

Slide 20 text

GCPERCENT ● GOGC=100 go run foo.go Duck Goose 50mb Live data + (GCPercent/100 * Live data) Trigger at 100mb

Slide 21

Slide 21 text

GCPERCENT ● GOGC=300 go run foo.go Duck Goose 50mb Trigger at 200mb

Slide 22

Slide 22 text

GCPERCENT ● GOGC=50 go run foo.go Duck Goose 50mb Trigger at 75mb

Slide 23

Slide 23 text

MAX HEAP SIZE ● Coming Soon.. maybe ● RSS = heap + stacks + globals ● Similar behavior with tuning GCPercent https://github.com/golang/go/issues/16843

Slide 24

Slide 24 text

THANK YOU!

Slide 25

Slide 25 text

REFERENCES ● The Garbage Collection Handbook: The Art of Automatic Memory Management ● Rick Hudson: https://www.youtube.com/watch?v=aiv1JOfMjm0&t=1s and https://blog.golang.org/ismmkeynote ● Bill Kennedy: https://www.ardanlabs.com/blog/2017/06/language-mechanics-on-memory-profiling.h tml ● Proposal on 1.9 algorithm: https://go.googlesource.com/proposal/+/master/design/17 503-eliminate-rescan.md

Slide 26

Slide 26 text

MARK AND SWEEP COLLECTION Stac k a Hea p Duck Goose a Goose Stop Moose Moose Gosling Gosling B B Shrink Stack?

Slide 27

Slide 27 text

OTHER GARBAGE COLLECTOR ALGORITHMS ● Mark-compact ● Copying ● Reference counting ● Manual garbage collection

Slide 28

Slide 28 text

TRICOLOR MARK AND SWEEP Stac k a a Hea p Duck Goose Stop Progra m B B Gosling Moose Duck Goose Gosling Moose Goose Goose Gosling Gosling Goose MooSe Strong Invariant: No black node can point to a white node

Slide 29

Slide 29 text

TRICOLOR MARK AND SWEEP A B Duc k Goo se Goo se A Gosl ing Moo se

Slide 30

Slide 30 text

ALLOCATION < 32 kB Size class 1 Size class 2 Size class 70 … Bitmap manager G Stack 2 … Value Value B B A G stack 1

Slide 31

Slide 31 text

NON-MOVING < 32 kB Size class 1 Size class 2 Size class 70 … Bitmap manager Value Value