Go > Memory Management > Garbage Collection > GC tuning best practices
Controlling Go's Garbage Collector with GOGC
This snippet demonstrates how to influence Go's garbage collector behavior using the GOGC
environment variable. It highlights how adjusting GOGC
can affect memory usage and garbage collection frequency.
Understanding GOGC
GOGC
is an environment variable that controls the initial garbage collection target percentage. It defaults to 100, meaning the GC aims to trigger when the heap size doubles since the last collection. Lowering GOGC
makes the GC more aggressive, collecting more frequently but potentially using more CPU. Increasing GOGC
makes the GC less aggressive, reducing CPU usage but potentially increasing memory footprint.
Code Snippet: Setting and Observing GOGC
This program does the following:
To run this code and observe the effects of GOGC
from the environment.GOGC
programatically to 50.GOGC
to it's original value.GOGC
:
Observe how go build main.go
GOGC
: ./main
GOGC=50
: GOGC=50 ./main
GOGC=200
: GOGC=200 ./main
HeapAlloc
changes after the garbage collection cycle with different GOGC
values.
package main
import (
"fmt"
"os"
runtime "runtime"
"time"
)
func allocate(size int) {
_ = make([]byte, size)
}
func main() {
initialGOGC := os.Getenv("GOGC")
fmt.Printf("Initial GOGC: %s\n", initialGOGC)
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("HeapAlloc (Before): %v bytes\n", m.HeapAlloc)
// Allocate a significant amount of memory
allocate(100 * 1024 * 1024) // 100MB
runtime.ReadMemStats(&m)
fmt.Printf("HeapAlloc (After Allocation): %v bytes\n", m.HeapAlloc)
time.Sleep(1 * time.Second)
runtime.GC()
runtime.ReadMemStats(&m)
fmt.Printf("HeapAlloc (After GC): %v bytes\n", m.HeapAlloc)
//Example of how to set the GOGC programatically
os.Setenv("GOGC", "50")
fmt.Printf("GOGC set programatically, new GOGC is: %s\n", os.Getenv("GOGC"))
newGOGC := os.Getenv("GOGC")
fmt.Printf("New GOGC is: %s\n", newGOGC)
//Reset GOGC to original value if it existed
if initialGOGC != "" {
os.Setenv("GOGC", initialGOGC)
} else {
os.Unsetenv("GOGC")
}
fmt.Printf("GOGC reset to : %s\n", os.Getenv("GOGC"))
}
Real-Life Use Case
In a high-traffic web server, aggressively reducing GOGC
might be beneficial to keep memory footprint low, preventing out-of-memory errors. Conversely, in a batch processing application where CPU usage is less critical than completion time, increasing GOGC
could allow the program to utilize more memory and complete tasks faster.
Best Practices
pprof
and metrics dashboards to track memory usage, GC frequency, and CPU utilization.GOGC
. Experiment with different values to find the optimal balance for your specific application.GOGC
value depends on the application's workload and resource constraints.GOGC
too low can lead to excessive GC overhead, while setting it too high can cause out-of-memory errors.
Interview Tip
Be prepared to discuss the trade-offs involved in tuning the garbage collector. Demonstrate an understanding of how GOGC
affects memory usage, CPU utilization, and overall application performance.
When to use them
Use GOGC
tuning when you observe performance bottlenecks related to either excessive memory usage or excessive garbage collection activity. This is especially relevant in resource-constrained environments or applications with strict performance requirements.
Memory Footprint
Lowering GOGC
generally decreases the memory footprint but increases CPU usage. Increasing GOGC
increases the memory footprint, but reduces CPU usage related to garbage collection.
Alternatives
Alternatives to GOGC tuning include:
Pros
GOGC
allows you to directly influence the garbage collector's behavior.
Cons
GOGC
involves trade-offs between memory usage and CPU utilization.GOGC
to ensure that you're achieving the desired results.
FAQ
-
What is the default value of GOGC?
The default value of GOGC is 100. -
What happens if I set GOGC to 0?
Setting GOGC to 0 disables garbage collection. This is generally not recommended, as it can lead to out-of-memory errors. -
How do I monitor the effects of GOGC tuning?
Use profiling tools likepprof
and metrics dashboards to track memory usage, GC frequency, and CPU utilization.