Course contents · Lesson 15 of 22

What Is Profiling? CPU and Memory Profiles Explained

Profiles · Lesson 15 of 22 · 11 min Engineer

It is Friday evening and the checkout service is slow. The dashboard shows CPU at 90%, and a trace shows that the request handler eats most of every request. You still do not know which line of code to change. So what is profiling? It is the technique that answers that last question: it measures where a running program spends its CPU time or memory, function by function, so you can point at the code that costs the most.

What a profile shows

A profile is a measurement of where a running program spends one resource, such as CPU time or memory, broken down by function and often by line of code. The tool that collects it is a profiler. You can profile a program while it serves real traffic.

To tie cost to code, a profiler records call stacks. A call stack is the chain of functions active at one moment: main called handleCheckout, which called parseJSON, which is running right now. The function at the top is the one executing; the ones below are waiting for it to return. If the four kinds of telemetry still blur together, the four signals lesson shows where profiles fit.

How a sampling CPU profiler works

There are two ways to build a profile. An instrumenting profiler adds timing code to the start and end of every function. That is precise but can slow the program several times over. A sampling profiler, the kind you run in production, leaves your code alone and takes snapshots instead.

The snapshot loop is simple. A timer interrupts the program at a fixed rate, typically about 100 times a second. Each time, the profiler copies the call stack of the running thread and adds one to the count for that exact stack. Go's built-in profiler samples at 100 Hz, so each sample stands for 10 ms of CPU time. Linux perf is often run at 99 Hz so samples do not fall into step with work that repeats 100 times a second.

Five-step flowchart: the program runs, a timer fires every 10 ms of CPU time, the profiler copies the call stack, identical stacks are counted, and the counts become a profile; a timeline below shows samples stopping while the program waits on the network
The profiler never times a function directly: it counts how often each stack is caught on the CPU, and the counts add up to a reliable picture.

This is statistics, not a stopwatch. A function found on top of the stack in 60% of the samples used about 60% of the CPU time the profiler saw. With thousands of samples the shares are dependable, though code that runs for less than one sample interval in total may not appear. Overhead stays small because the profiler does a little work per 10 ms of CPU used, not per request.

Table of ten sampled call stacks, each starting main then handleCheckout, tallied into bars: parseJSON on top in 6 of 10 samples, applyDiscount in 3 and renderReceipt in 1
parseJSON is on top in 6 of 10 samples, so it gets 60% of the CPU time, while main is in every sample yet does almost no work itself.

One property matters more than any other. A CPU profile is an on-CPU profile: its timer counts CPU time, not clock time. While a thread waits for the database or the network, it is off the CPU, the timer does not advance and no samples are taken. So a CPU profile shows code that is computing and says nothing about time spent waiting. Forgetting this is the most common way to misread one.

Here is a real CPU profile of a small Go program that imitates a checkout handler. It ran for ten seconds under Go's built-in profiler, and go tool pprof printed this summary (three lines about table filtering are trimmed):

$ go tool pprof -top -cum -nodecount=4 cpu.pprof
File: checkout
Type: cpu
Time: 2026-11-24 16:40:23 IST
Duration: 10.19s, Total samples = 9.19s (90.21%)
      flat  flat%   sum%        cum   cum%
         0     0%     0%      8.96s 97.50%  main.handleCheckout
         0     0%     0%      8.96s 97.50%  main.main
         0     0%     0%      8.96s 97.50%  runtime.main
         0     0%     0%      6.05s 65.83%  main.parseJSON

Read the header first. The run lasted 10.19 s and the samples add up to 9.19 s of CPU, so the program kept about 90% of one core busy. flat is self time: samples where the function itself was on top of the stack. cum, short for cumulative, is total time: self time plus everything the function called. main.parseJSON has zero flat time but 65.83% cum, so the cost is inside the JSON library code it calls.

Memory profiles: allocated versus in use

A heap profile, or memory profile, records which code allocates memory. The heap is the memory a program requests while it runs, for data whose size or lifetime is not known in advance. Heap profiles are sampled too: Go records about one allocation per 512 KiB allocated and scales the numbers up.

Every heap profile answers two questions. Allocated (alloc_space) is everything the code has allocated since the program started, including memory already freed. In use (inuse_space) is what is still held, which in Go means as of the last garbage collection. The same checkout program shows the difference:

$ go tool pprof -sample_index=alloc_space -top -nodecount=3 heap.pprof
Type: alloc_space
Showing nodes accounting for 2016.62MB, 99.33% of 2030.29MB total
      flat  flat%   sum%        cum   cum%
 2011.62MB 99.08% 99.08%  2011.62MB 99.08%  strings.(*Builder).WriteString (inline)
       5MB  0.25% 99.33%    15.52MB  0.76%  main.parseJSON
         0     0% 99.33%  2027.13MB 99.84%  main.handleCheckout

$ go tool pprof -sample_index=inuse_space -top heap.pprof
Type: inuse_space
Showing nodes accounting for 2.66MB, 100% of 2.66MB total

In ten seconds the program allocated about 2 GB, almost all of it in strings.Builder calls made while building receipts, yet only 2.66 MB is still in use. That is churn: short-lived garbage that keeps the garbage collector busy, fixed by allocating less. A memory leak looks different: in-use memory for the same function keeps climbing between profiles taken hours apart.

On-CPU, off-CPU and other profile types

A slow request often spends most of its time off-CPU: blocked on a disk read, a network reply, a lock or a sleep. A CPU profile cannot see that time, so other profile types exist:

  • Wall-clock profile: samples threads whether running or waiting, so it shows where elapsed time goes (async-profiler for Java has a wall mode).
  • Off-CPU profile: records the stack each time a thread blocks, and for how long, for example with the eBPF tool offcputime on Linux.
  • Goroutine or thread profile: a snapshot of what every thread, or goroutine (Go's lightweight thread), is doing right now, such as 10,000 goroutines stuck on one call.
  • Lock and block profiles: where code waits for a lock or a channel, and how long. In Go both are off until you switch them on.

A simple rule: if CPU is high, start with a CPU profile. If requests are slow but CPU is low, the time is off-CPU, so reach for a wall-clock, off-CPU or lock profile.

Profiles versus metrics and traces

Three cards for one slow checkout: the metric says CPU is at 90%, the trace says the handler span takes 0.9 s of a 1.2 s request, and the profile says parseJSON uses 66% of the CPU
Each signal narrows the question, and only the profile names the code.

The metric says that something is wrong and how badly. A trace says where inside the request the time went. The profile says why that part is expensive: parseJSON and the code it calls hold two thirds of the CPU samples.

Profiles have limits too. A profile is merged over many requests, so it cannot explain one slow request the way a trace can, and it knows nothing about errors or users. Open it after a metric and a trace have pointed you at a service.

Check yourself

A CPU profile of a slow API shows little CPU use, yet requests take two seconds. Why might the profile miss the cause?

The time is probably off-CPU, waiting on a database, network or lock, and a CPU profile only samples code that is running on a CPU.

A function has 0% flat time but 70% cum time. What does that tell you?

It does no expensive work itself; the cost is in the functions it calls, so look at its callees.

A heap profile shows huge alloc_space but small, steady inuse_space for one function. Is that a leak?

No, it is churn: the memory is allocated and freed quickly, which costs garbage-collector time but does not grow.