Tensorflow
GPU
OOM error
Machine Learning
Deep Learning

Tensorflow OOM on GPU

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A TensorFlow GPU out-of-memory error means the runtime cannot satisfy a memory allocation request on the selected GPU. Sometimes that means the model or batch is genuinely too large. Sometimes it means TensorFlow grabbed memory aggressively, another process is using the device, or one particular operation created a temporary activation tensor that exceeded the remaining space. The fix depends on which of those cases you are actually hitting.

Understand What TensorFlow Is Allocating

GPU memory is consumed by more than model weights. During training, TensorFlow also needs memory for activations, gradients, optimizer state, input batches, and temporary work buffers created by kernels. That is why a model that looks small on disk can still fail at runtime with an OOM error.

A common mistake is to focus only on parameter count. For convolutional networks, transformers, and sequence models, intermediate activations often dominate memory usage, especially with large batch sizes.

TensorFlow May Reserve Most GPU Memory Up Front

TensorFlow's GPU guide notes that, by default, TensorFlow maps nearly all visible GPU memory for the process. That behavior reduces fragmentation, but it can be surprising on shared workstations because it looks like TensorFlow is using the entire card even before training starts.

If you want TensorFlow to grow memory usage as needed instead, enable memory growth before creating tensors or models.

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices('GPU')
4if gpus:
5    for gpu in gpus:
6        tf.config.experimental.set_memory_growth(gpu, True)

This does not solve every OOM, but it prevents TensorFlow from immediately reserving the whole device when the process starts.

The First Real Lever Is Batch Size

When an OOM appears during training, reducing batch size is usually the fastest and most reliable fix.

python
1import tensorflow as tf
2import numpy as np
3
4x = np.random.rand(128, 64).astype("float32")
5y = np.random.rand(128, 1).astype("float32")
6
7dataset = tf.data.Dataset.from_tensor_slices((x, y)).batch(8)
8
9model = tf.keras.Sequential([
10    tf.keras.layers.Dense(256, activation="relu"),
11    tf.keras.layers.Dense(256, activation="relu"),
12    tf.keras.layers.Dense(1)
13])
14
15model.compile(optimizer="adam", loss="mse")
16model.fit(dataset, epochs=2, verbose=0)

If batch size 64 fails and batch size 8 works, you have confirmed that the training step footprint, not startup configuration alone, is the main issue.

Put a Hard Limit on GPU Memory When Needed

For local development on a shared GPU, it can be useful to cap TensorFlow to a fixed amount of memory.

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices('GPU')
4if gpus:
5    tf.config.set_logical_device_configuration(
6        gpus[0],
7        [tf.config.LogicalDeviceConfiguration(memory_limit=4096)]
8    )

This must also happen before the GPU is initialized. A hard cap is helpful when you need one process to stay within a bounded share of the device, though it obviously reduces the maximum workload that can fit.

Reduce Activation and Optimizer Footprint

If lowering batch size too much hurts throughput or convergence, the next step is to reduce per-step memory demand.

Useful options include:

  1. smaller input resolution
  2. fewer layers or narrower hidden sizes
  3. mixed precision on supported hardware
  4. gradient checkpointing or recomputation in advanced setups
  5. choosing an optimizer with lower state overhead

Mixed precision can be particularly effective on modern GPUs.

python
from tensorflow.keras import mixed_precision

mixed_precision.set_global_policy("mixed_float16")

This is not universal magic, but it often lowers memory usage enough to make a previously failing model train.

Check for Other GPU Consumers

OOM errors are sometimes caused by the environment rather than the model. Another notebook, a browser using GPU acceleration, or a second training job may already be occupying VRAM.

Outside TensorFlow, nvidia-smi is the quickest way to see whether the GPU is actually free. If the card is nearly full before your process starts training, changing code may not be the main fix.

Use Profiling When the Failure Is Not Obvious

If the model still fails after basic reductions, profile memory usage instead of guessing. TensorFlow's profiler includes memory analysis features that help identify peak allocations and the operations responsible for them. That is especially useful when only one layer or one specific input shape causes the crash.

This matters because not all OOMs are equal. One may happen during model initialization. Another may happen only during backpropagation. Another may appear only when validation runs with a larger image size than training. Profiling tells you where the real spike occurs.

Common Pitfalls

  • Assuming model file size is the same thing as training-time GPU memory usage.
  • Forgetting that TensorFlow may reserve most visible GPU memory by default.
  • Trying to fix a training-step OOM without first testing a smaller batch size.
  • Setting memory growth or logical device limits after the GPU has already been initialized.
  • Blaming TensorFlow when another process is already consuming a large share of the GPU.

Summary

  • TensorFlow GPU OOM means the runtime could not satisfy a memory allocation on the device.
  • The biggest levers are usually batch size, model footprint, and startup memory configuration.
  • 'set_memory_growth helps on shared machines by avoiding eager reservation of all GPU memory.'
  • Hard caps and mixed precision are useful secondary tools when you need tighter control.
  • When the cause is unclear, profile memory usage instead of guessing which layer or step is responsible.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.