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

Garbage Diving for Gophers

Garbage Diving for Gophers

What is Garbage collection and why do we need it? How does Garbage collection happen and when? How can we as engineers be more sympathetic to the machines our code runs on? If you've ever found yourself asking any of these questions or are curious to know more, this is the talk for you.

Code can be found here: https://github.com/sleepypioneer/garbage_diving_for_gophers


Jessica Greene

May 26, 2020


  1. Garbage Diving for Gophers A collection of thoughts on the

    Go Garbage Collector
  2. What this talk aims to deliver 3. Tools to work

    with for observing GC Tracing and looking at when the GC is being done. Measuring stop the world time. 4. Techniques to make your code more machine sympathetic How we can reduce GC and better manage how it runs in our programs 1. General overview of what the garbage collection is Why we have it, the main mechanics & 2. How Go’s garbage collector works Why it exists in the language, how it works under the hood, and when it happens This talk is a work in progress, I am very open to feedback! :) 2
  3. Hello! I am Jessica Greene Backend Software Engineer at Ecosia.org

    You can find me at @sleepypioneer 3
  4. 1. What is Garbage collection And why do we need

  5. Memory is not endless - it has to be released

    at some point 5
  6. Heap // escapes to heap func slicer(a, b int) []int

    { return []int{a, b} } Where your allocations go 6 Stack // doesn't escape to heap func slicey(a, b int) { foo := []int{a} foo = append(foo, b) } We can use escape analysis to find out where a value is being store!` -gcflags “-m”
  7. 7 How do I know whether a variable is allocated

    on the heap or the stack? ¶ “From a correctness standpoint, you don't need to know” - godocs
  8. How to clean up allocations • Manually - developer adds

    code to remove unused values • A built in operation to check values and clean up those no longer needed ie garbage collector 8
  9. Want big impact? Use big image. 9 (Garbage Collector) 2.

    Go’s GC • Concurrent • Low latency
  10. 10 Go 1.5 - Go is building a garbage collector

    (GC) not only for 2015 but for 2025 and beyond https://blog.golang.org/go15gc
  11. How Garbage collection Happens 11 Sweep • Actually cleaning up

    the unused values PACER • Runs in the background • Determines length of collection • Makes sure GC is finished before we run out of memory Marking • Inspecting values to see if they are referenced • Marking them as in use
  12. Let’s see it in Action 12

  13. 13 End of first garbage run 2MB in-use 2MB in-use

    2MB newly-allocation If GC Percentage is set to 100 (default) then the heap will be doubled
  14. 14 On the next run the GC finishes with 5MB

    in use 5MB in-use 5MB in-use 5MB newly-allocation So the pacer will assign 5MB to the heap so the next collection will run at or just before 10MB
  15. Garbage Collection ~ Marking Mark set up Turn the write

    barrier on Sweep termination Stop the world time! All application goroutines running must be stopped Inspection & marking Starts with root objects pointing to variables on the heap Traverses along memory pointers Marks those in use Runs concurrently! Mark termination Turn the write barrier off Stop the world time! Calculate next collection 15
  16. Garbage Collection ~ Concurrency 16 G1 G2 G3 G4 P

    P P P G1 G2 G3 G4 P P P P Gc G2 G3 G4 P P P P 25% CPU used for GC Gc Gc G3 G4 P P P P More CPU used for GC (mark assist) Gc Gc G3 G4 P P P P Gc Gc G3 G4 P P P P
  17. Sweeping Happens when goroutines allocate new values to the Heap

  18. Let’s see it in Action 18

  19. 19 https://github.com/sleepypioneer/garbage_diving_for_gophers

  20. 20 “A careful (informed) programmer can reduce the garbage collection

    overhead dramatically by using the language well; “ https://golang.org/doc/faq#garbage_collection
  21. Go Trace tool ~ https://godoc.org/golang.org/x/net/trace 21

  22. Optimising for GC Developer sympathy in a age of endless

    resources 22
  23. Reduce the number of values on the heap Be mindful

    of when you allocate REDUCE THE NO. OF GOROUTINES ALLOCATING Less goroutines allocating memory == less complex memory management Reduce the amount of allocated memory Ensure allocated values are small 23
  24. The future of GO’s GC?

  25. What we have covered ✘ What is GC & why

    we need it ✘ Go’s GC and why it’s included in the language ✘ The phases of GC & how it interacts with goroutines 25 ✘ Stop the world time & latency ✘ A few ideas on how to optimise our code to be more sympathetic to the GC ✘ Edge cases, non web server examples
  26. Credits Special thanks to Daria who collaborated on all the

    research included in this talk, to Bill Kennedy for being a big inspiration & to Ronna Steinberg for her constant support! ✘ Presentation template by SlidesCarnival ✘ Photographs by Unsplash ✘ Gophers from Ashley Mcnamara, Egon Elbe, and Marcus Olsson 26
  27. 27 Resources: ✘ https://www.ardanlabs.com/blog/2018/12/garbage-collection-in-go-p art1-semantics.html ✘ https://blog.golang.org/go15gc ✘ https://dave.cheney.net/tag/godebug

  28. 28 THANKS FOR LISTENING! Any questions? You can find me

    at @sleepypioneer & jessica.greene@ecosia.org