ProgramGuru

Garbage Collection in Go

Start with Garbage Collection in Go

Go uses a garbage collector to reclaim managed heap objects that can no longer be reached by the program.

What you’ll learn

  • Explain the main purpose of garbage collection in go.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Garbage collection removes the need to manually free ordinary Go objects. It does not guarantee a collection at the instant the final reference disappears. Values may remain reachable through slices, maps, closures and other objects.

Garbage collection is not a substitute for closing files, database connections or network sockets when appropriate. Use defer and the resource-specific close method for such resources. Most Go applications should leave collector scheduling to the runtime rather than force it in normal request paths.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for garbage collectionReachability matters; exact collection timing is not promised.Allocate objectStep 1Use while reachableStep 2May become unreachableStep 3GC may reclaim laterStep 4Reachability matters; exact collection timing is not promised.
Reachability matters; exact collection timing is not promised.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"runtime"
)

func main() {
	data := make([]int, 3)
	data[0] = 7
	fmt.Println("First element:", data[0])
	fmt.Println("Go manages ordinary heap objects:", true)
	runtime.KeepAlive(data)
}

Output

First element: 7
Go manages ordinary heap objects: true

Trace what the program does

The slice is allocated and used while reachable. runtime.KeepAlive explicitly keeps the value considered live through that call, but this demonstration does not claim exactly when any garbage collection runs.

Avoid common mistakes

  • Do not infer exact garbage-collection timing from a short program.
  • Garbage collection does not automatically close every external resource at the point your code stops using it.

Try it yourself

Open a temporary file with os.CreateTemp, defer its Close and Remove calls, and explain why resource cleanup differs from memory reclamation.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What does Go garbage collection primarily reclaim?
Choose one answer. You can try again.

Answer and explanation

Unreachable managed heap objects

Correct. The slice is allocated and used while reachable.

What you should remember

You have practiced Garbage Collection in Go in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Structs →