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

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.

GopherCon Israel

February 11, 2019
Tweet

More Decks by GopherCon Israel

Other Decks in Programming

Transcript

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

    objects • Freeing space used by dead objects
  2. WHEN IS GARBAGE GENERATED func main() { NewDuck() } type

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

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

    a = NewDuck() a.Tweet() } type Bird interface { Tweet() } Stack a Heap Duck Bird
  6. GC PACER Core Core OS Tread OS Tread G o

    G o G o G o G o G o G o G o
  7. 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
  8. TIMELINE Mark and Sweep Tricolor, Concurrent Mark and Sweep Weak

    Invariant Tricolor Mark and Sweep 1.5 1.9 1.0
  9. BENCHMARKS • Be wary of toy Garbage Collection tests •

    Don’t behave the same a “real” programs • locality effects • Optimum dataset for any algorithm
  10. 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
  11. TIME GC STW- 100 microseconds Humans process visual stimuli -

    13 ms Speed of light from Tel Aviv to San Francisco - 40ms
  12. GCPERCENT • GOGC=100 go run foo.go Duck Goose 50mb Live

    data + (GCPercent/100 * Live data) Trigger at 100mb
  13. MAX HEAP SIZE • Coming Soon.. maybe • RSS =

    heap + stacks + globals • Similar behavior with tuning GCPercent https://github.com/golang/go/issues/16843
  14. 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
  15. MARK AND SWEEP COLLECTION Stac k a Hea p Duck

    Goose a Goose Stop Moose Moose Gosling Gosling B B Shrink Stack?
  16. 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
  17. TRICOLOR MARK AND SWEEP A B Duc k Goo se

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

    Size class 70 … Bitmap manager Value Value