DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
HardTheory

GOMEMLIMIT and GOGC: Tuning Go Memory Management

21 views

Problem Statement

Your Go service runs in a 2GB container but keeps getting OOM killed. Configure GOMEMLIMIT and GOGC to prevent this while maintaining performance.

GOGC (Garbage Collection Percentage)

// GOGC=100 (default): GC triggers when heap doubles
// GOGC=50: GC triggers when heap grows 50%
// GOGC=200: GC triggers when heap triples

import "runtime/debug"

func init() {
    debug.SetGCPercent(50) // More frequent GC, less memory
}

// Or via environment:
// GOGC=50 ./myapp

GOMEMLIMIT (Go 1.19+)

import "runtime/debug"

func init() {
    // Soft memory limit - GC becomes aggressive near limit
    debug.SetMemoryLimit(1.5 * 1024 * 1024 * 1024) // 1.5GB
}

// Or via environment:
// GOMEMLIMIT=1500MiB ./myapp

Container Configuration

# Kubernetes deployment
containers:
- name: myapp
  resources:
    limits:
      memory: 2Gi
  env:
  - name: GOMEMLIMIT
    value: "1800MiB"  # Leave headroom for OS
  - name: GOGC
    value: "100"

The Old Ballast Technique (Obsolete)

// Before GOMEMLIMIT, teams used "ballast":
var ballast = make([]byte, 10<<30) // 10GB unused allocation

// This tricked GC into running less often
// Now obsolete - use GOMEMLIMIT instead

Debugging Memory Issues

# Enable GC tracing
GODEBUG=gctrace=1 ./myapp

# Output example:
# gc 5 @0.5s 2%: 0.5+2.0+0.5 ms clock, 4.0+0/2.0/0+4.0 ms cpu, 
#    4->5->2 MB, 5 MB goal, 4 P

# Meaning:
# 4->5->2 MB = heap before GC -> after GC -> live data
# 2% = percentage of CPU spent on GC

Best Practices

  • Set GOMEMLIMIT to 80-90% of container memory
  • Leave room for OS file cache and overhead
  • Monitor with runtime.ReadMemStats()
  • Use pprof heap profiles to find allocations

Sample Test Cases

Case 1
Input
GOMEMLIMIT=1GiB, heap_growth
Expected Output
GC triggers to stay under 1GiB
Case 2
Input
GOGC=50, baseline_heap=100MB
Expected Output
GC triggers at 150MB heap

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
21
Likes
0
Solutions
0
Comments
0

Category

Backend Engineering

Languages

Go