Go and the garbage collection

1

Transcript of Go and the garbage collection

Go &The garbage collection

“Joris BonnefoyDevOps @ OVH

@devatoria

2

IntroductionReminder of basic algorithmic structures

» Objects have a pointer to the next element of the list

» The last element have a null pointer

Linked list 4

» Objects have pointers to the next and the previous element of the list

» The first element have a null previous pointer» The last element have a null next pointer

Doubly linked list 5

» Objects have pointers to the next and the previous element of the list

» The first element have a previous pointer to the last element of the list

» The last element have a next pointer to the first element of the list

Circular doubly linked list 6

A simple problemHow to free a dereferenced object?

How to free a dereferenced object? 8

How to free a dereferenced object? 9

How to free a dereferenced object? 10

A solutionThe garbage collection

The garbage collection

» John McCarthy - 1959 - Lisp» Opposite of manual memory management» Used to free unused allocated memory» Can handle more types of resources (network

sockets, database handlers, …) using finalization and region-based allocation

12

The garbage collection

» No dangling pointers» No memory leaks» No double free

» Can reduce performances» Stop-the-world issues» Can be unpredictable

13

Two types of garbage collector

Reference counting

How reference counting is implemented?

» Each object has a count of other objects referencing it

» An object is considered as garbage when its counter reach 0

16

How reference counting is implemented? 17

How reference counting is implemented? 18

Disadvantages of reference counting

» Sensitive to cycle referencing

19

Disadvantages of reference counting

» Increased space overhead» Must be atomic» Not real-time

20

Tracing garbage collection

Concept of tracing garbage collection

» Determine which objects are reachable from a certain “root” object

» Unreachable objects are considered as garbage» Can use a lot of algorithms

22

Two ways of reachability

» The object is reachable from a certain root» Local variables» Parameters» Global variables

» The object is reachable via another reachable object

23

Tracing garbage collectionThe naïve mark-and-sweep algorithm

The naïve mark-and-sweep algorithm

» Two stages algorithm» First, run through the root set tree and mark

reachable objects» Second, run through the whole memory, sweep

unmarked objects and clear mark of marked objects

25

The naïve mark-and-sweep algorithm disadvantages

» Must run through the memory twice» Must freeze the execution of the program to avoid

mutation of the working set

26

Go and the garbage collectionThe tri-color marking

The tri-color marking

» Proposed by Dijkstra in 1978» Tri-color» Concurrent» Mark-and-sweep

28

The tri-color marking concept

» Heap is a graph of connected objects» Uses 3 colored sets

» White: contains objects to collect» Gray: contains objects reachable from the root

but not yet scanned» Black: contains reachable and scanned objects

29

The tri-color marking steps

» At the beginning, everyone is in the white set» Root objects are moved in the gray set» A root object is scanned

» Its references are colored gray, while the root object itself is colored black

» Its references are scanned too and moved to the black set» Once the gray set is empty, the remaining objects in the white set

are freed and sets are recolored

30

The tri-color marking by example 31

The tri-color marking by example 32

The tri-color marking by example 33

The tri-color marking by example 34

The tri-color marking by example 35

The tri-color marking by example 36

The tri-color marking by example 37

The algorithm invariant

“The tri-color marking invariant

No black object may contain a pointer to a white object

39

How to always satisfy the invariant?

» Stop the world until GC is complete» Use a write barrier

» Function executed by the mutator to tell the GC that a pointer has been changed

» Enforce an object to be gray when it is created during the garbage collector cycle

40

How to always satisfy the invariant? 41

Go back to lower versions of GoThe garbage collector review by Richard Hudson

Go 1.4 and lower

» Stop-the-world from the beginning of the cycle to the end

» High GC latency» Not concurrent

43

Go 1.5 (and actual) garbage collector

» Made by Richard Hudson and his team» Concurrent» Low latency» Decrease program performances but increase

throughput

44

Go 1.5 new garbage collector 45

Benchmarking 46

Go 1.6 and further

» Improvements on when to start a GC cycle, used metrics, etc.

» Predictive

47

What’s new in Go 1.8?The garbage collector review

Actual garbage collector pain point 49

New type of write barrier

» Hybrid write barrier» Dijkstra (1978)» Yuasa (1990)

» New algorithm invariant (weak invariant)» No need to rescan the whole memory» Reduces GC cycle

» 100 microseconds for the worst case» 50 microseconds for 95 percentile

50

“Any white object pointed to by a black object is reachable from a grey object via a chain of

white pointers (it is grey-protected).

51

Go 1.8 hybrid write barrier 52

Languages without garbage collector

Rust

» Based on ownership and borrowing» Every value has a scope» Passing or returning a value means transferring its ownership» When a scope ends, owned values are destroyed» Everything is checked at compile time

» Everything is allocated on the stack by default» Using stack frames» Stack frames are freed on function return

» Can be allocated on the heap using boxes

54

Rust - Ownership 55

Rust - Ownership 56

Rust - Ownership 57

Rust - Borrowing 58

“Thank you for listening

59

References

» Pusher https://blog.pusher.com/golangs-real-time-gc-in-theory-and-practice/» Golang new GC proposal

https://github.com/golang/proposal/blob/master/design/17503-eliminate-rescan.md» Go GC 1.5 blog post https://blog.golang.org/go15gc» GopherCon 2015 GC Latency talk https://www.youtube.com/watch?v=aiv1JOfMjm0» Dijkstra 1978 GC paper http://dl.acm.org/citation.cfm?id=359655» Rust lang https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html

60