codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

62 / 72 topics
60Performance Tuning Techniques61Memory Management in Go62Garbage Collection
Tutorials/Go (Golang)/Garbage Collection
🐹Go (Golang)

Garbage Collection

Updated 2026-04-20
3 min read

Introduction

Garbage collection (GC) is an automatic memory management process that helps developers manage memory allocation and deallocation without manual intervention. In Go, the garbage collector plays a crucial role in optimizing performance and ensuring efficient use of resources. This tutorial will delve into the intricacies of garbage collection in Go, including its mechanisms, tuning tips, and best practices for performance optimization.

Understanding Garbage Collection in Go

Go's garbage collector is designed to be simple yet effective, aiming to provide predictable performance without manual memory management. The GC in Go is a concurrent mark-and-sweep collector that operates on the heap, which stores all dynamically allocated objects.

Key Concepts

  • Heap: The area of memory where all dynamic allocations occur.
  • Mark Phase: Identifies reachable objects from the roots (e.g., global variables, local variables).
  • Sweep Phase: Frees up memory occupied by unreachable objects.
  • Generations: Objects are categorized into generations based on their age. New objects start in Gen0 and move to higher generations if they survive multiple GC cycles.

How Garbage Collection Works

  1. Allocation: When a new object is created, it is allocated on the heap.
  2. Marking: The garbage collector identifies all reachable objects from roots.
  3. Sweeping: Unreachable objects are deallocated, and their memory is made available for future allocations.

Tuning Garbage Collection

Go provides several environment variables to tune the behavior of the garbage collector. Understanding these settings can help optimize performance for specific workloads.

GOGC

The GOGC variable controls the percentage of heap growth that triggers a GC cycle. The default value is 100, meaning a GC cycle is triggered when the heap size increases by 100%. Lowering this value makes the GC more aggressive but can increase CPU usage.

// Set GOGC to 50 to trigger GC at 50% growth
import "os"
func init() {
    os.Setenv("GOGC", "50")
}

GOMAXPROCS

The GOMAXPROCS variable sets the maximum number of operating system threads that can be executing simultaneously. Increasing this value allows more concurrent GC cycles, which can improve performance on multi-core systems.

// Set GOMAXPROCS to 4
import "runtime"
func init() {
    runtime.GOMAXPROCS(4)
}

GODEBUG

The GODEBUG variable provides various debugging options for the garbage collector. For example, setting gcstoptheworld=2 can help diagnose GC-related issues.

// Enable detailed GC logs
import "os"
func init() {
    os.Setenv("GODEBUG", "gcstoptheworld=2")
}

Best Practices

Minimize Allocation and Deallocation

Reducing the frequency of allocations and deallocations can improve performance. Use pre-allocated slices, maps, and other data structures to minimize heap growth.

// Pre-allocate a slice with a fixed capacity
func processItems(items []string) {
    results := make([]string, 0, len(items))
    for _, item := range items {
        result := transform(item)
        results = append(results, result)
    }
    return results
}

Avoid Global Variables

Global variables can increase the GC's workload by making more objects reachable. Use local variables and function parameters to limit the scope of allocations.

// Function with limited scope for allocations
func processItem(item string) string {
    // Process item without global state
    return transform(item)
}

Profile and Monitor

Use profiling tools like pprof to monitor GC performance and identify bottlenecks. Analyzing heap profiles can help optimize memory usage.

// Import pprof for profiling
import (
    "net/http"
    _ "net/http/pprof"
)

func init() {
    go func() {
        http.ListenAndServe("localhost:6060", nil)
    }()
}

Advanced Techniques

Custom Allocators

For performance-critical applications, consider implementing custom allocators. This can provide more control over memory allocation and deallocation.

// Simple custom allocator example
type Allocator struct {
    pool [][]byte
}

func (a *Allocator) Allocate(size int) []byte {
    for i := range a.pool {
        if len(a.pool[i]) >= size {
            b := a.pool[i]
            a.pool = append(a.pool[:i], a.pool[i+1:]...)
            return b[:size]
        }
    }
    return make([]byte, size)
}

func (a *Allocator) Free(b []byte) {
    a.pool = append(a.pool, b)
}

Finalizers

Finalizers are functions that run when an object is garbage collected. They can be used for cleanup tasks but should be used sparingly as they can introduce performance overhead.

// Using finalizers for resource cleanup
import "runtime"

type Resource struct {
    fd int
}

func (r *Resource) Close() error {
    // Perform cleanup
    return nil
}

func NewResource(fd int) *Resource {
    r := &Resource{fd: fd}
    runtime.SetFinalizer(r, func(r *Resource) { r.Close() })
    return r
}

Conclusion

Garbage collection is a powerful feature in Go that simplifies memory management and improves performance. By understanding the mechanisms of GC and tuning its behavior with environment variables, developers can optimize their applications for better efficiency. Following best practices and using advanced techniques like custom allocators can further enhance performance in critical scenarios.

Remember to profile and monitor your application regularly to identify areas for improvement and ensure that the garbage collector is working effectively.


PreviousMemory Management in GoNext CGO (Calling C Code from Go)

Recommended Gear

Memory Management in GoCGO (Calling C Code from Go)