Find out which code uses the time and memory: CPU profiles, flame graphs and continuous profiling.
Profiling measures where a running program spends its time and memory, down to the function or line. Metrics tell you that something is slow. A profile tells you which code is making it slow.
Your dashboards say the p99 latency of a service doubled. Tracing says the time is spent inside one service. Now what? You need to look inside that process. A profiler does this: it records which functions are running (and who called them) many times a second, and adds up the result.
A sampling profiler looks at the program's call stack about 100 times a second and counts what it sees. It is cheap, and safe enough to run in production. An instrumenting profiler adds code around every function to time it exactly, but slows the program down, sometimes a lot, and can change what it measures.
| Kind | Question it answers | Typical clue |
|---|---|---|
| CPU | Which code burns the CPU? | A wide frame of pure computation |
| Wall-clock / off-CPU | Where does the program wait? | Time in I/O, sleeps, locks |
| Heap / memory | What holds the memory? | An object type that keeps growing |
| Allocations | Who creates the most objects? | A hot loop that allocates, causing GC pressure |
| Lock contention | Which locks make threads wait? | Many threads blocked on one mutex |
| I/O | Which reads and writes are slow? | Many small disk or network calls |
| GC | How long are the pauses? | Latency spikes at regular intervals |
| Sampling | Instrumenting | |
|---|---|---|
| How it works | Looks at the stack many times a second | Adds timing code around functions |
| Overhead | Low (often a few percent) | Can be high |
| Accuracy | Statistical: fine for hot spots, can miss tiny functions | Exact counts, but may change behaviour |
| Use in production | Yes, often continuously | Rarely |
A made-up profile with fixed numbers. Amdahl's law: overall speedup = 1 รท (1 โ share ร improvement), where share is the part you improve. A real profile has thousands of frames and many requests, but the lesson is the same.
If a part takes a share s of the total time and you make it k times faster, the overall speedup is 1 รท ((1 โ s) + s รท k). Making a 5% part infinitely fast gives at most 1.05ร. Making a 50% part twice as fast gives 1.33ร.
| Term | Meaning in easy words |
|---|---|
| Profiler | A tool that records where a program spends time or memory |
| Sampling | Looking at the call stack at regular moments and counting |
| Instrumentation | Adding code to measure each function exactly |
| Flame graph | A chart where width is time and stacked boxes are the call stack |
| Self time | Time in a function itself, not in the functions it calls |
| Total time | Self time plus the time of everything it calls |
| Hot spot | A small part of the code that uses a large part of the time |
| Wall-clock time | Real elapsed time, including waiting |
| CPU time | Time actually running on a processor |
| Heap profile | A snapshot of what objects hold memory |
| Allocation profile | Which code creates objects, and how many |
| Contention | Threads waiting for each other, usually on a lock |
| Continuous profiling | Collecting low-overhead profiles all the time in production |
| Amdahlโs law | The limit on overall speedup when you improve only part of a task |
| Choice | We gain | We lose |
|---|---|---|
| Higher sample rate | More detail | More overhead |
| Always-on profiling | History to look back at | Storage and a little CPU |
| Profile in staging | Safe and easy | May not match production |
| Profile in production | Real behaviour | Needs care for overhead and privacy |
| Optimise early | Fast from the start | Complex code for gains you may not need |