TensorFlow
CUDA
Out of Memory
GPU
Deep Learning

TensorFlow CUDA_ERROR_OUT_OF_MEMORY

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

CUDA_ERROR_OUT_OF_MEMORY in TensorFlow means the framework asked the GPU driver for more memory than was currently available. The direct cause is usually easy to state, but the real fix depends on why memory usage is so high in the first place: model size, batch size, other GPU processes, eager accumulation, or TensorFlow's allocator behavior.

The Most Common Cause: Batch Size

The fastest lever to test is batch size. Bigger batches mean more activations, gradients, and temporary tensors, all of which consume GPU memory.

A simple training loop often fails at one batch size and succeeds at a smaller one with no other changes:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(512, activation="relu"),
5    tf.keras.layers.Dense(10),
6])
7
8model.compile(
9    optimizer="adam",
10    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
11)
12
13x = tf.random.normal((1024, 100))
14y = tf.random.uniform((1024,), maxval=10, dtype=tf.int32)
15
16model.fit(x, y, batch_size=32, epochs=1)

If memory fails, try batch_size=16 or 8 before doing anything more exotic.

Let TensorFlow Grow GPU Memory Gradually

By default, TensorFlow may reserve a large chunk of GPU memory up front. Enabling memory growth can make development and multi-process GPU use much less painful:

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

This should be done before TensorFlow starts placing tensors on the GPU.

Check for Other GPU Users

Sometimes your model is not the real problem. Another notebook, training job, browser tab, or visualization process may already be using most of the GPU. Check the device with nvidia-smi.

If another process is occupying the memory, reducing your batch size may only hide the conflict instead of solving it.

Model and Input Size Matter Too

Large image resolutions, long sequences, and deep models consume memory quickly. A convolutional network on 224x224 inputs has a very different memory profile from the same model on 1024x1024 inputs.

When debugging, try reducing one variable at a time:

  • batch size
  • input resolution
  • model width or depth
  • sequence length

That makes it easier to identify which factor is driving the memory spike.

Mixed Precision Can Help

If your hardware supports it, mixed precision can cut memory usage significantly by storing parts of the computation in lower precision:

python
1import tensorflow as tf
2
3from tensorflow.keras import mixed_precision
4
5mixed_precision.set_global_policy("mixed_float16")

This is not a universal fix, but it is one of the most useful memory-saving tools in modern GPU training.

Clear Stale State in Notebooks

In notebooks, repeated model creation can leave behind graphs, variables, or references that keep memory alive longer than expected. Clearing the Keras backend between experiments helps:

python
import tensorflow as tf

tf.keras.backend.clear_session()

This is especially useful when you rerun cells many times while changing model definitions.

Common Pitfalls

The most common mistake is assuming the GPU has a memory leak when the batch size is simply too large for the model and input shape.

Another issue is forgetting that other processes may already own most of the GPU. TensorFlow only sees what is left.

Developers also enable memory growth after TensorFlow has already initialized the GPU, which is too late for the setting to take effect.

Summary

  • 'CUDA_ERROR_OUT_OF_MEMORY means TensorFlow requested more GPU memory than was available.'
  • Reduce batch size first because it is the fastest and most common fix.
  • Enable TensorFlow memory growth before GPU initialization.
  • Check for competing GPU processes with tools such as nvidia-smi.
  • Consider mixed precision, smaller inputs, or a lighter model if the workload genuinely exceeds the device capacity.

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.