The garbage collector / Maya Rosecrance

The garbage collector / Maya Rosecrance

We’ll go through a high level overview of how the golang garbage collector works. We’ll also touch on the one knob we have to tweak, the garbage collector GCPercent and when it might make sense to do so.

As a golang developer, I usually assume that something is going to clean up memory and my programs will ‘just work’. But it’s worth it to take a peek under the covers to understand the tradeoffs that have been made for performance.
In 20 minutes we’re going to run through a very high level overview of how the golang garbage collector works. I’ll illustrate what happens when a ‘stop-the-world’ is called and we’ll reason about how it impacts running go programs. We’ll also touch on the one knob we have to tweak, the garbage collector GCPercent and when it might make sense to use it.

C1508d6ff2ed86b2b4fedef9a3fe558f?s=128

GopherCon Israel

February 11, 2019
Tweet

Transcript

  1. THE GOLANG GARBAGE COLLECTOR Maya Rosecrance

  2. MEMORY MANAGEMENT SYSTEM • Memory Allocation • Marking of live

    objects • Freeing space used by dead objects
  3. CONTENTS • Garbage generation • Garbage Collection • Measuring performance

    • Configuring the Garbage Collector
  4. WHEN IS GARBAGE GENERATED func main() { NewDuck() } type

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

    Duck struct {} //go:noinline func NewDuck() *Duck { return &Duck{} } Stack & Heap Duck
  6. 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
  7. WHEN IS GARBAGE GENERATED func main() { var a Bird

    a = NewDuck() a.Tweet() } type Bird interface { Tweet() } Stack a Heap Duck Bird
  8. TRIGGER THE GARBAGE COLLECTOR Duck Goose 50mb Trigger at 100mb

  9. GC PACER Core Core OS Tread OS Tread G o

    G o G o G o G o G o G o G o
  10. CONCURRENT, TRICOLOR MARK AND SWEEP *NON-GENERATIONAL, NON-MOVING

  11. 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
  12. TIMELINE Mark and Sweep Tricolor, Concurrent Mark and Sweep Weak

    Invariant Tricolor Mark and Sweep 1.5 1.9 1.0
  13. TRADEOFFS • GC throughput • Program throughput • Pause times

    (Latency)
  14. MEASUREMENT Performance

  15. BENCHMARKS • Be wary of toy Garbage Collection tests •

    Don’t behave the same a “real” programs • locality effects • Optimum dataset for any algorithm
  16. 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
  17. TIME Stack Allocation Alone - 10 ms Original - 300

    ms
  18. TIME GC STW- 100 microseconds Humans process visual stimuli -

    13 ms Speed of light from Tel Aviv to San Francisco - 40ms
  19. GARBAGE COLLECTOR KNOBS Tweak the GC

  20. GCPERCENT • GOGC=100 go run foo.go Duck Goose 50mb Live

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

    at 200mb
  22. GCPERCENT • GOGC=50 go run foo.go Duck Goose 50mb Trigger

    at 75mb
  23. MAX HEAP SIZE • Coming Soon.. maybe • RSS =

    heap + stacks + globals • Similar behavior with tuning GCPercent https://github.com/golang/go/issues/16843
  24. THANK YOU!

  25. 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
  26. MARK AND SWEEP COLLECTION Stac k a Hea p Duck

    Goose a Goose Stop Moose Moose Gosling Gosling B B Shrink Stack?
  27. OTHER GARBAGE COLLECTOR ALGORITHMS • Mark-compact • Copying • Reference

    counting • Manual garbage collection
  28. 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
  29. TRICOLOR MARK AND SWEEP A B Duc k Goo se

    Goo se A Gosl ing Moo se
  30. 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
  31. NON-MOVING < 32 kB Size class 1 Size class 2

    Size class 70 … Bitmap manager Value Value