4. » Objects have a pointer to the next element of the
list
» The last element have a null pointer
Linked list 4
5. » 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
6. » 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
12. 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
13. The garbage collection
» No dangling pointers
» No memory leaks
» No double free
» Can reduce performances
» Stop-the-world issues
» Can be unpredictable
13
16. 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
22. 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
23. 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
25. 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
26. 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
27. Go and the garbage collection
The tri-color marking
28. The tri-color marking
» Proposed by Dijkstra in 1978
» Tri-color
» Concurrent
» Mark-and-sweep
28
29. 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
30. 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
39. “The tri-color marking invariant
No black object may contain a pointer to a
white object
39
40. 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
42. Go back to lower versions of Go
The garbage collector review by Richard Hudson
43. Go 1.4 and lower
» Stop-the-world from the beginning of the cycle to
the end
» High GC latency
» Not concurrent
43
44. Go 1.5 (and actual) garbage collector
» Made by Richard Hudson and his team
» Concurrent
» Low latency
» Decrease program performances but increase
throughput
44
50. 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
51. “
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
54. 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