Level: AdvancedInterview

Go Performance Interview Questions: Memory and pprof (Part 23)

Updated 9 min read
Go Performance Interview Questions featured image: pprof, escape analysis and GC cards

These Go performance interview questions cover benchmarks, pprof, the tracer, escape analysis, allocations, the garbage collector, GOGC and GOMEMLIMIT. We gathered them from public interview lists, coding rounds and community threads. Then we rewrote them in plain words and grouped them by idea.

This post is questions only, so answer each one in your own words first. When you need help, read Go profiling with pprof, Go garbage collector and memory and Go benchmarks. Also, finish Part 22 on gRPC first, because many performance problems show up between services.

How to Use These Go Performance Interview Questions

Answer each of these Go performance interview questions out loud, then prove one answer with a benchmark or a profile. Interviewers want to hear that you measure first and change code second. Finally, try the scenario questions, since they come from real slow services.

The table below lists each section and its number of questions.

SectionWhat it testsQuestions
Measure firstBenchmarks, pprof and the tracer16
MemoryAllocations and escape analysis15
Garbage collectorGOGC, GOMEMLIMIT and pauses15
CPU and data structuresSlices, maps, strings and inlining15
Concurrency and IOLocks, goroutines and buffers14
Performance coding tasksProfile and fix programs13
Go Performance Interview Questions diagram showing six question areas from measuring to coding tasks
The six areas covered in this question list, from measuring to coding tasks.

Go Performance Interview Questions on Measuring

Before you answer, study Go profiling with pprof. These Go performance interview questions start with tools, because guessing wastes time.

  1. Why should you measure before you optimise?
  2. Which profiles can pprof collect, such as CPU, heap, goroutine, block and mutex?
  3. How do you add the pprof HTTP endpoints to a service, and then fetch a profile?
  4. Why should pprof endpoints never be public on the internet?
  5. How do you read a flame graph, and what does a wide box mean?
  6. What is the difference between flat and cumulative time in pprof?
  7. What does the inuse_space view show compared with alloc_space?
  8. How do you compare two heap profiles to find a leak?
  9. What does the execution tracer show that pprof does not?
  10. How do you run a benchmark with memory stats and many counts?
  11. Why should you compare benchmark runs with benchstat?
  12. What is profile-guided optimization, and how does a default.pgo file help?
  13. How do you profile a short CLI tool instead of a server?
  14. What is continuous profiling, and why do teams run it in production?
  15. Scenario: when a service uses 80 percent CPU after a release, how would you find the hot path?
  16. Scenario: when a teammate claims a change is faster but shows no numbers, what would you ask for?

Go Performance Interview Questions on Memory

Before you answer, study Go pointers explained and Go slices and arrays. Allocations drive both speed and GC load, so these Go performance interview questions go into detail.

  1. What is the difference between stack and heap allocation in Go?
  2. What is escape analysis, and how does the compiler decide where a value lives?
  3. How do you see escape decisions with go build -gcflags=-m?
  4. Why does returning a pointer to a local variable often move it to the heap?
  5. Why can storing a value in an interface cause an allocation?
  6. How does preallocating a slice with make reduce allocations?
  7. Why does appending in a loop without capacity copy the data several times?
  8. How can a small slice of a large array keep the whole array alive?
  9. Why can a map never shrink its memory after you delete keys?
  10. How does strings.Builder avoid extra copies?
  11. When does converting between string and []byte allocate?
  12. How does struct field order change the size of a struct?
  13. How do you count allocations in a test with testing.AllocsPerRun?
  14. Scenario: when memory keeps growing because a cache holds sub-slices of large buffers, how would you fix it?
  15. Scenario: when a hot handler allocates 40 times per request, how would you cut that number?

Go Performance Interview Questions on the Garbage Collector

Before you answer, study Go garbage collector and memory. GC questions are common for senior roles, so these Go performance interview questions check how you tune it.

  1. How does the Go garbage collector work at a high level?
  2. Why do people call the Go collector concurrent mark and sweep?
  3. Why does the GC need a write barrier while your code runs?
  4. What does GOGC control, and what does the default of 100 mean?
  5. What does GOMEMLIMIT do, and which problem did it solve in Go 1.19?
  6. Why can a too low memory limit make the GC run all the time?
  7. How do you read GC stats with GODEBUG=gctrace=1?
  8. What are stop-the-world pauses, and how long are they in modern Go?
  9. How does reducing allocations help the GC more than tuning flags?
  10. When does sync.Pool help, and when does it hurt?
  11. What is the Green Tea collector experiment in Go 1.25 about?
  12. How should a container memory limit relate to GOMEMLIMIT?
  13. Why is calling runtime.GC by hand almost always a mistake?
  14. Scenario: when a pod gets OOM-killed even though heap profiles look small, what would you check?
  15. Scenario: when GC takes 30 percent of CPU time, what would you change first?

Go Performance Interview Questions on CPU and Data Structures

Before you answer, study Go maps with examples and Go strings with examples. Small choices in data structures add up, so these Go performance interview questions focus on the hot path.

  1. Why is iterating over a slice of structs often faster than over a slice of pointers?
  2. How does CPU cache locality affect Go code, for example in tight loops?
  3. When is a map slower than a sorted slice with binary search?
  4. How do you give a map a size hint, and why does it help when you know the count?
  5. Why is string concatenation with + in a loop slow?
  6. How do strconv functions beat fmt.Sprintf for number formatting?
  7. What is function inlining, and how do you see which calls the compiler inlines?
  8. What is bounds check elimination, and how can a hint help?
  9. Why is reflection slow, and how do code generators avoid it?
  10. How does encoding/json speed compare with generated encoders?
  11. When does a switch beat a map lookup for a small set of keys?
  12. Why can defer cost almost nothing in modern Go?
  13. How do you avoid copying a large struct on every call?
  14. Scenario: when JSON encoding takes half of the CPU profile, what options would you consider?
  15. Scenario: when a lookup table of 20 items uses a map, how would you test whether a slice is faster?

Concurrency and IO Performance

Before you answer, study Go sync package and Go reader and writer interfaces. These Go performance interview questions check the costs that come from locks, goroutines and IO.

  1. How does lock contention show up in a mutex profile?
  2. When does sharding a lock improve throughput?
  3. Why can too many goroutines slow a program down instead of speeding it up?
  4. How do you size a worker pool for CPU-bound work?
  5. How does buffered IO with bufio reduce system calls?
  6. Why should you reuse an http.Client and keep connections alive?
  7. How does GOMAXPROCS affect throughput in a container?
  8. What is false sharing, and how can padding fix it?
  9. When are atomics faster than a mutex, and when are they not?
  10. How do you batch database writes to cut round trips?
  11. How do you find goroutines blocked on channels with a block profile?
  12. Why can logging in a hot path hurt latency?
  13. Scenario: when p99 latency is high but average latency looks fine, how would you investigate?
  14. Scenario: when adding more workers makes a job slower, what would you check?

Go Performance Interview Questions for Coding Rounds

Before you answer, write each task by hand and back every claim with a benchmark. Interviewers watch your method, not only the final speed.

  1. Write a benchmark that compares + and strings.Builder for 1000 joins.
  2. Reduce allocations in a function that builds a slice of 10,000 items.
  3. Reorder struct fields to shrink the struct size.
  4. Add pprof to a server and capture a 30 second CPU profile.
  5. Find and fix a memory leak caused by a growing global map.
  6. Replace fmt.Sprintf with strconv in a hot loop and measure it.
  7. Use sync.Pool for buffers in a JSON handler and benchmark it.
  8. Batch 1000 inserts into fewer database calls.
  9. Write a benchmark with b.Loop and report allocations.
  10. Compare a map and a sorted slice for 16 keys.
  11. Read a 1 GB file line by line with steady memory.
  12. Scenario: when an interviewer gives you a slow function and ten minutes, what steps would you follow?
  13. Scenario: when a fix is 5 percent faster but much harder to read, would you merge it?

Code Reading Questions

Read each program carefully and predict the output before you run it. Then explain the result in your own words.

package main

import (
    "fmt"
    "testing"
)

func appendNoCap(n int) []int {
    var s []int
    for i := range n {
        s = append(s, i)
    }
    return s
}

func appendWithCap(n int) []int {
    s := make([]int, 0, n)
    for i := range n {
        s = append(s, i)
    }
    return s
}

var sink []int

func main() {
    a := testing.AllocsPerRun(100, func() { sink = appendNoCap(1000) })
    b := testing.AllocsPerRun(100, func() { sink = appendWithCap(1000) })
    fmt.Println(a, b)
}
  1. Why is the first number much larger than the second, and what does each allocation do?
  2. Why does the code store the result in a package level sink variable?

The second program measures two structs that hold the same fields.

package main

import (
    "fmt"
    "unsafe"
)

type Loose struct {
    Active bool
    ID     int64
    Ready  bool
    Count  int32
}

type Tight struct {
    ID     int64
    Count  int32
    Active bool
    Ready  bool
}

func main() {
    fmt.Println(unsafe.Sizeof(Loose{}), unsafe.Sizeof(Tight{}))
    fmt.Println(unsafe.Alignof(int64(0)), unsafe.Offsetof(Loose{}.ID))
}
  1. What sizes print on a 64-bit machine, and where does the padding go in Loose?
  2. When does this kind of saving matter, for example in a slice of a million items?

Quick Revision Table for Go Performance Tools

Use the table below as a final check before your interview. It maps each tool to the question it answers.

ToolAnswersCommand or call
BenchmarkHow fast is this code?go test -bench=. -benchmem
CPU profileWhere does time go?go tool pprof cpu.out
Heap profileWhere does memory go?/debug/pprof/heap
Execution traceWhy do goroutines wait?go tool trace
Escape analysisDoes this value escape?go build -gcflags=-m
GC traceHow often does the GC run?GODEBUG=gctrace=1

Also, many interviewers ask you to compare stack and heap allocation directly. The diagram below shows the difference side by side.

Go performance diagram comparing stack and heap allocation
Stack values vanish when the function returns, while heap values stay until the garbage collector frees them.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go performance interview questions cover measuring, memory, the GC, data structures, concurrency and coding tasks.
  • Measure with benchmarks and pprof before you change any code.
  • Fewer allocations mean less GC work, so preallocate and avoid needless escapes.
  • GOGC trades memory for CPU, and GOMEMLIMIT sets a soft memory cap.
  • Pick data structures for the hot path, and keep structs compact.
  • Watch locks, goroutine counts and IO calls, because they often hide the real cost.

What’s Next?

By now, you have Go performance interview questions for profiling, memory and the GC. Study the answers in your own notes, then check them against the Go interview topics study guide.

Next up: Go microservices interview questions (Part 24)