OOM - term AI

Out of Memory (OOM Error) is a critical state in which a computational device cannot allocate the requested amount of RAM for the current operation, leading to an immediate process halt.

How the OOM Mechanism Works

A neural network operates by manipulating tensors — multi-dimensional tables of numbers. The entire lifecycle of a model in GPU memory consists of three main components competing for space:

Static Weights: The architecture itself and trained parameters. For modern models (LLMs), this is the foundation, occupying tens of gigabytes.
Dynamic Activations: Intermediate computation results. They exist only during forward and backward passes. Their volume directly depends on the batch size (the number of examples processed simultaneously).
State Cache (KV-Cache): A specific memory consumer for generative models. To generate text token by token, the model saves the memory of previous words. During long dialogues, this cache grows to gigantic sizes, often displacing everything else from memory.

The error occurs at the moment when the total volume of these structures exceeds the limit of the installed video card. The process is immediately interrupted by the CUDA or ROCm kernel, returning a fatal error to the system.

Main Scenarios of Failure

Training and Fine-tuning

Here, OOM most often strikes activations. A researcher tries to increase the Batch Size to speed up convergence. However, each example in the batch requires storing its own set of gradients and intermediate vectors. Upon reaching a certain threshold, an attempt to calculate gradients for the backward pass leads to an instant crash, even if the model’s weights fit into memory.

Inference and the «Time Bomb»

In production, OOM is more insidious. A model might successfully start and process 100 short requests. But in generation mode (when the model writes a long response), memory leaks gradually.

Context Problem: In multi-turn chats, each user response is glued to the history. The KV-cache grows with every iteration.
Critical Mass: The system works stably until a single request with a context of 30,000 tokens arrives, or enough long sessions accumulate. Memory runs out not smoothly, but abruptly, causing a refusal to service new users.

Why Free Memory Might Not Exist in the Monitor

Users often face a paradox: a system monitor (e.g., nvidia-smi) shows 6 GB free out of 24 GB, but PyTorch throws a CUDA out of memory error.

This happens for three reasons:

Memory Fragmentation: Memory may be free «on average,» but represent a patchwork quilt of small occupied sections. A huge tensor (e.g., an attention matrix) needs one solid, continuous block. If it doesn’t exist — there will be an OOM.
Reservation (Caching): Frameworks (PyTorch, TensorFlow) use a caching allocator. They do not return memory to the operating system after use but keep it ready for the next tensor. The monitor sees this as «occupied by the process,» although it is free inside the framework.
CUDA Graphs: To speed up inference, the computation graph is fixed in memory. Memory for the worst-case scenario (maximum batch size) is reserved in advance and locked, even if current requests are short.

Engineering Methods of Combat

Defeating OOM by simply buying new hardware is impossible — models grow faster than GPU capabilities. Engineers use the following strategies:

Data Compression (Quantization)

Transferring weights from FP32 (32-bit) format to FP16, BF16, or INT4. This is the fastest way to gain 50–75% of memory with virtually no loss in answer accuracy.

Activation Checkpointing

A radical method for training. We deliberately delete intermediate activations from memory during the forward pass to save space. During the gradient training stage, we recalculate these activations from scratch. We lose in speed (computation time), but gain the ability to train models that physically do not fit into VRAM.

Context Management (for LLMs)

To prevent OOM during generation:

Sliding Window: The model forgets the beginning of the conversation, keeping only the last N tokens in focus.
Summarization: Instead of storing the entire message history, old chats are compressed by a compact neural network into a short vector-summary.

PagedAttention and Virtualization

A technology implemented in the vLLM engine. It borrows the idea of virtual memory from operating systems. Instead of allocating each request a huge chunk of memory «with a margin» (maximum length), the KV-cache is allocated in small blocks (pages) only as needed. This eliminates fragmentation and allows running 2–4 times more concurrent requests on a single card.

OOM is an inevitable barrier in AI development. It signals that the task architecture requires more resources than are physically available. A modern MLOps engineer fights OOM not by increasing the GPU budget, but through deep optimization: using torch.compile, quantization, implementing PagedAttention, and strict control over the length of input and output sequences. In an environment where model sizes are measured in hundreds of billions of parameters, the ability to manage memory becomes more important than the ability to write a network architecture.

Let's start a conversation

Start Scaling Today

Contact our experts to select the optimal hardware configuration and calculate rental pricing for your AI workloads.

Location: Moscow, ext.ter.g., sh. Ochakovskoe, 28, p.2
GPU Servers High-density compute
Облачная аренда Быстрый старт ИИ моделей
Enterprise SLA Guaranteed availability
Low Latency Optimal network routes