ProgramGuru

Performance and Memory Optimization

Start with Performance and Memory Optimization

Learn performance and memory optimization through an executable example, then connect the small demonstration to production code.

What you’ll learn

  • Explain the main purpose of performance and memory optimization.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Performance tuning starts with measurements: go test -bench . -benchmem, CPU profiles and allocation profiles. Use simple optimizations after identifying a bottleneck.

Preallocating known slice capacity can avoid repeated growth allocations, but it consumes memory up front. Benchmark realistic workloads before committing an optimization.

Visualize how it works

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

Visual explanation for memory optimizationOptimize a measured requirement, not a hunch.Measure baselineStep 1Profile bottleneckStep 2Change codeStep 3Re-measureStep 4Optimize a measured requirement, not a hunch.
Optimize a measured requirement, not a hunch.

Tools, syntax and practical notes

Useful tools: go test -bench . -benchmem, go test -cpuprofile cpu.out, go test -memprofile mem.out, and go tool pprof.

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"

func squares(n int) []int {
	values := make([]int, 0, n)
	for i := 1; i <= n; i++ {
		values = append(values, i*i)
	}
	return values
}
func main() {
	nums := squares(4)
	fmt.Println("Values:", nums, "Length:", len(nums), "Capacity:", cap(nums))
}

Output

Values: [1 4 9 16] Length: 4 Capacity: 4

Trace what the program does

The slice begins with length zero and capacity four. Four appended squares fill its reserved storage without requiring capacity growth.

Avoid common mistakes

  • Allocations and escape decisions depend on compiler optimization; do not claim a fixed heap layout.
  • A benchmark must avoid accidental dead-code elimination or unrepresentative workloads.

Try it yourself

Benchmark this implementation against a nil-slice version using go test -benchmem.

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

Check your understanding

What should guide a production optimization?
Choose one answer. You can try again.

Answer and explanation

A measured problem and re-measurement

Correct. The slice begins with length zero and capacity four.

What you should remember

You have practiced Performance and Memory Optimization 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: Cross-Compilation →