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
Tweet

More Decks by Jessica Greene

Other Decks in Technology

Transcript

  1. 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
  2. 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”
  3. 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
  4. 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
  5. Want big impact? Use big image. 9 (Garbage Collector) 2.

    Go’s GC • Concurrent • Low latency
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 20 “A careful (informed) programmer can reduce the garbage collection

    overhead dramatically by using the language well; “ https://golang.org/doc/faq#garbage_collection
  13. 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
  14. 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
  15. 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