Garbage Collection is a process of freeing memory that is allocated and contains some data that is not being used.

Notes:

  • by doing escape analysis GC mechanism decides what goes to heap and what stays on stack
  • use -gcflags '-m' flag to get escape analysis info, e.g. go run -gcflags '-m' main.go
  • another way to look into what GC is doing during runtime is to run program with GODEBUG=gctrace=1
  • GC runs consurrently with main program running
  • main running program is called “mutator”
  • Golang Garbage Collection uses “tricolor algorithm” otherwise known as tricolor mark and sweep algorithm.
  • GC represents data in the heap as graph of elements/objects
  • channels are also garbage collected when they become unreachable, even if channel is not closed

Sources: