AIToday
AI Coding AssistantsQiita 機械学習Published: Dec 5, 2026, 10:01 JST

PyTorch memory: reserved is not used, measure before cutting batch size

Send on LINE
PyTorch memory: reserved is not used, measure before cutting batch size

3 Key Points

  1. What happened

    A measurement routine for PyTorch training reports allocated memory (torch.cuda.memory_allocated()), reserved memory (torch.cuda.memory_reserved()) and peak memory, and is called right after forward, backward and optimizer.step.

  2. Why it matters

    If allocated memory climbs with every update, the likely cause is retained tensors or the computation graph; if only reserved memory grows and then levels off, it is probably caching or variation in allocation size.

Ask AI

Ask the AI about this article. Questions and answers are published on this page

Context & Analysis

This is a counterpoint to a common reflex: seeing a big GPU memory number and immediately reducing the batch size. Its argument is that PyTorch's allocator holds memory for reuse, so the reserved figure can be large without any leak. To separate the two, the author proposes a small report function that prints allocated, reserved and peak GiB, built on torch.cuda.memory_allocated(), torch.cuda.memory_reserved() and torch.cuda.max_memory_allocated().

The placement of those measurements matters. The suggested practice is to call the report right after forward, right after backward and right after optimizer.step, and to compare the first update with steady state because optimizer state may be created on the first step. Observations map to next steps: allocated memory rising on every update points to held tensor references or a retained computation graph; reserved memory rising and then stabilizing points to caching or allocation-size variation; a peak at backward points to activations, gradients or activation checkpointing; a jump on the first step points to optimizer state. It also shows loss_history.append(loss.detach().item()) for keeping only loss values, while cautioning that item may involve GPU synchronization, so logging frequency should be considered.

The author closes with a practical caveat: PyTorch's counters do not cover Windows shared GPU memory, CUDA contexts or allocations by external libraries, so OS-side dedicated and shared memory should also be watched. The code is presented as a measurement example, not as results from a specific environment.

FAQ
Does high reserved memory mean there is a memory leak?
No. A large reserved amount alone does not mean a memory leak, because PyTorch's allocator reserves memory for reuse separately from memory actually occupied by tensors.
When should peak memory statistics be reset?
Call torch.cuda.reset_peak_memory_stats() before the section you want to measure, so the peak figure reflects that interval. Synchronization is for measurement only and can hurt performance if inserted into every production step.
Why compare the first update with steady state?
Optimizer state may be created on the first update, so the initial step and the steady state should be compared separately.
Qiita 機械学習Read Original Article
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

By signing up you agree to the Terms of Service and Privacy Policy.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Related Articles

Next articleGPT-6 Luna judge calibration lifts κ from 0.04 to 0.66