Google Colaboratory
GPU memory
TensorFlow
clear memory
runtime optimization

How to clear GPU memory WITHOUT restarting runtime in Google Colaboratory Tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Google Colab, TensorFlow often keeps GPU memory reserved even after a model finishes, which makes iterative experimentation frustrating. The good news is that you can usually release a large part of that memory without restarting the entire runtime. The bad news is that not every form of GPU memory retention can be cleared perfectly from inside the same process.

Why Memory Appears to Stay Allocated

TensorFlow uses a GPU allocator that often keeps memory for reuse instead of returning it to the system immediately. That behavior improves performance, but it makes notebooks feel "stuck" after large experiments.

Memory can remain occupied because of:

  • Python references that still point to models or tensors
  • Keras global state that still knows about old graphs and layers
  • live datasets, callbacks, or optimizers
  • TensorFlow's allocator caching memory for reuse

So clearing memory is usually about releasing objects and clearing framework state, not just calling one command.

The First Cleanup Sequence

A good first pass is:

python
1import gc
2import tensorflow as tf
3
4# Delete large model-related objects first.
5del model
6
7# Clear Keras and TensorFlow backend state.
8tf.keras.backend.clear_session()
9
10# Force Python garbage collection.
11gc.collect()

This works well when the memory is still held by Python references and Keras backend state.

If you created several large objects, delete them all explicitly before calling clear_session().

Rebuild Models Inside Functions

One of the easiest ways to leak memory in a notebook is to keep building models in the global scope. A cleaner pattern is to create and train models inside a function so local references disappear more naturally.

python
1import tensorflow as tf
2
3
4def train_once():
5    model = tf.keras.Sequential([
6        tf.keras.layers.Dense(128, activation="relu"),
7        tf.keras.layers.Dense(1),
8    ])
9    model.compile(optimizer="adam", loss="mse")
10    return model
11
12
13model = train_once()

When you are done, deleting model and clearing the session becomes more effective because fewer global references survive.

Check for Lingering References

If memory does not drop, it often means something still references the model, tensors, dataset, or optimizer state. Common culprits include:

  • notebook variables from earlier cells
  • closures that captured tensors
  • callback objects
  • training histories storing large arrays

In those cases, deleting only model is not enough. You may also need:

python
del history
del dataset

followed by:

python
tf.keras.backend.clear_session()
gc.collect()

Verify the GPU State

In Colab, nvidia-smi is useful for checking what the system still sees:

bash
!nvidia-smi

If memory remains high after cleanup, ask whether TensorFlow is caching it for reuse or whether another process still holds it.

In many notebook sessions, the memory does not fall all the way to zero, but subsequent TensorFlow jobs can still reuse it successfully.

When It Does Not Fully Release

Sometimes TensorFlow or another library keeps enough process-level GPU state that a full release is unrealistic without restarting the runtime. This is especially common after repeated experiments with large models, mixed libraries, or native-code resources.

So the honest answer is:

  • you can often recover enough memory for continued work
  • you cannot always force a perfect full reset without restarting

That is a Colab and process-lifecycle limitation more than a notebook command problem.

Preventing the Problem Up Front

You can make future runs less painful by enabling memory growth when appropriate:

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

This reduces the chance that TensorFlow grabs all available GPU memory at once. It does not solve every memory issue, but it often makes notebook iteration smoother.

Common Pitfalls

The biggest mistake is assuming clear_session() alone will free everything. It helps, but Python references and dataset objects may still keep memory alive.

Another mistake is building many models in notebook cells without deleting old references. Colab encourages experimentation, but global notebook state accumulates quickly.

People also misread nvidia-smi. Memory still shown there may be allocator-reserved memory that TensorFlow can reuse, not always a sign of an unrecoverable leak.

Finally, sometimes the only real fix is a runtime restart. If native state is badly fragmented or stuck, forcing a perfect in-process reset may not be realistic.

Summary

  • Delete model-related objects, then call tf.keras.backend.clear_session() and gc.collect().
  • Clear all lingering references, not just the model variable.
  • Build models inside functions when possible to reduce notebook-state buildup.
  • Use nvidia-smi to verify what the GPU still sees.
  • You can often recover enough memory without restarting, but a full reset is not always possible inside one Colab runtime.

Course illustration
Course illustration

All Rights Reserved.