Junior — Middle
How is automatic memory management implemented in the Go language?
sobes.tech AI
Answer from AI
In the Go language, automatic memory management and freeing are implemented using a built-in garbage collector. Go uses a tracing and reference counting algorithm that periodically scans memory, identifying objects that are no longer referenced, and frees them. This allows developers not to worry about manual memory management, reducing the likelihood of leaks and errors.
Example:
package main
func main() {
s := make([]int, 1000000) // allocate memory
_ = s
// After exiting the scope of s, the garbage collector will automatically free the memory
}