Middle+
How is garbage collection implemented in Go and in which memory area does it operate?
sobes.tech AI
Answer from AI
Garbage Collector (GC) in Go is a trigger-based, concurrent, incremental, and parallel garbage collector with a mark-and-sweep algorithm.
It operates on the heap — the memory area where dynamically allocated objects are placed. The stack and static data are not subject to garbage collection.
Main stages of GC operation:
- Marking: GC traverses all roots (stack, global variables) and marks all reachable objects.
- Sweep: all objects not marked as reachable are considered garbage and are freed.
Go GC runs concurrently with the program execution, minimizing pauses. It is optimized for low latency and scales on multi-processor systems.
This is roughly how GC in Go is structured, allowing efficient memory management without explicit deallocation.