Read the story behind a GPU memory spike
A visual guide makes training-memory behavior easier to diagnose.

A memory timeline can explain an out-of-memory error better than a single utilization number.
The PyTorch memory tutorial explains how to record allocation history and inspect it in a visualizer. It follows model creation, inputs, forward passes, and retained activations to show why memory can stay occupied after a Python variable changes.
This is a better starting point than guessing which optimization to enable. Persistent parameters, temporary tensors, and activations have different lifetimes. Seeing those lifetimes helps distinguish a normal training peak from accidental retention or an unnecessarily large intermediate result.
Record a short, representative training segment and compare its phases with the walkthrough. Keep the model and batch small enough to understand before adding more moving parts. The source also connects the visual trace to estimates of the memory a run will require.
A memory spike tells you which phase needs attention
GPU memory use changes during a training step. Loading weights, building activations, computing gradients and maintaining optimizer state do not all happen at the same moment or have the same scaling behaviour. A single peak number can tell you that a run does not fit, but it cannot explain which part of the process caused the limit.
The useful debugging approach is to relate memory observations to the training phases. That gives a more precise next experiment than reducing every setting at once and hoping the error disappears.
Establish a minimal reproducible run
Use a small fixed batch and a known sequence length. Confirm that the problem occurs consistently before changing settings. Record the model, precision, optimizer and relevant configuration so that comparisons use the same starting point.
Separate memory reserved by the runtime from memory actively used by tensors where the tooling makes that distinction. Allocator behaviour can make the apparent footprint confusing, especially after several experiments have run in the same process.
Change the variable that matches the bottleneck
If activations dominate, batch size, sequence length or activation checkpointing may be relevant. If optimizer state dominates, a different optimizer representation or parameter-efficient approach may be worth studying. The correct intervention depends on the observed phase and the training objective.
Do not assume that reducing trainable parameters eliminates all memory pressure. Frozen model weights and forward-pass activations still exist. Similarly, a lower-precision configuration can change numerical behaviour and should be evaluated rather than treated as a free switch.
Watch for retained computation
A training loop can accidentally keep references to tensors or computation graphs that should be released. Logging entire tensors, accumulating outputs without detaching them or retaining per-step objects can create gradual growth. A run that fits initially and fails much later deserves a different investigation from one that fails on the first batch.
Track memory across several steps after initialization. Look for a stable pattern versus a continuing increase, then inspect the code that stores results between iterations.
Validate the optimized run
After a memory change, check that the training calculation still means what you intended. Smaller batches, accumulation and checkpointing settings can interact with throughput and experiment configuration. Preserve the effective setup in the run record.
Measure time as well as memory. A configuration that saves enough memory to fit may require additional computation, which can still be a worthwhile trade-off.
The goal of memory profiling is not merely to suppress an out-of-memory error. It is to understand the resource structure of the experiment well enough to choose a change that preserves the learning objective and makes the run feasible within the available hardware.
Source: Visualize and understand GPU memory in PyTorch ↗ · qgallouedec. How we write


