tensorflow-GPU OOM issue after several epochs
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A TensorFlow job that runs for a few epochs and then suddenly fails with GPU out-of-memory is usually not just "the model is too large." If the same model fits at the beginning but crashes later, memory is often accumulating somewhere across steps or epochs. The real fix is to find what is growing over time: tensors kept alive in Python, retraced graphs, cached dataset state, or large activations combined with an aggressive batch size.
Why OOM Can Appear Late Instead Of Immediately
If the model is simply too big for the GPU, you usually fail on the first forward or backward pass. A delayed OOM suggests memory growth.
Common causes include:
- storing predictions or losses in a Python list every step
- recreating
@tf.functiongraphs repeatedly with changing input shapes - building new models inside a loop without clearing the old one
- dataset pipelines that cache large tensors unexpectedly
- fragmentation or aggressive preallocation on the GPU
That is why "it crashes after epoch 6" is an important clue. Something is not being reused or released correctly.
Enable Safer GPU Memory Behavior
A good first step is to tell TensorFlow not to grab all GPU memory up front.
This does not solve every OOM problem, but it makes memory use more incremental and can reduce confusion while you debug.
Avoid Accumulating Tensors Across Epochs
One of the easiest ways to leak memory is to store tensors every step without converting or discarding them.
Problematic pattern:
loss here is still a TensorFlow tensor, and holding onto many of them can keep computation history or device memory alive longer than expected.
Safer version:
If you only need scalar logging, store plain Python numbers instead of tensors.
Watch Out For Repeated Model Creation
Another common mistake is creating a new model in a loop, especially during experiments or cross-validation, without clearing the previous graph state.
clear_session() is important when many model objects are created in the same Python process.
Reduce Peak Memory Pressure
Even when the late OOM is caused by growth, peak usage still matters. The most direct levers are:
- lower the batch size
- reduce image or sequence dimensions
- use mixed precision when appropriate
- simplify the model or checkpoint fewer activations
Mixed precision can help on supported GPUs:
That often lowers activation memory significantly, though you should still validate numerical behavior.
Dataset Pipelines Matter Too
A tf.data pipeline can also cause problems if it caches very large tensors in memory or expands data aggressively.
This is usually sensible. But cache() should be used carefully. Caching a large transformed dataset in memory can make a job appear fine at first and then run out once the cache fills.
Common Pitfalls
The most common mistake is treating a late OOM exactly like an immediate OOM. If the crash happens only after several epochs, look for growth, not just peak size.
Another issue is storing tensors for logging or debugging instead of converting them to plain values. That keeps extra memory alive longer than needed.
It is also easy to rebuild models repeatedly in notebooks and long-running processes without clearing the old state.
Finally, do not forget the input pipeline. A dataset cache or oversized prefetch configuration can contribute to memory pressure just as much as the model itself.
Summary
- A delayed TensorFlow GPU OOM usually points to memory growth over time, not only a large model.
- Check for stored tensors, repeated graph creation, and problematic dataset caching.
- Enable GPU memory growth to make debugging behavior clearer.
- Use
clear_session()when creating many models in one process. - Lower batch size, use mixed precision, and simplify the input pipeline when peak memory is still too high.
Related reading
- TensorFlow - Difference between tf.keras.layers.Layer vs tf.keras.Model
- TensorFlow - Low GPU usage on Titan X
- Tensorflow - Minimize with Complex Gradient
- TensorFlow - numpy-like tensor indexing
- Tensorflow-Lite pretrained model does not work in Android demo
- TensorFlow-Slim data provider for in-memory dataset
- Tensorflow2 warning using tffunction
- Tensorflow - casting from int to float strange behavior
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.