How to clear GPU memory in tensorflow 2?
Quick answer
TensorFlow allocates all GPU memory up front by default. To avoid that, enable memory growth with tf.config.experimental.set_memory_growth(gpu, True) before any tensors are created, or cap it with set_virtual_device_configuration. To release memory held within a process, del the model and call tf.keras.backend.clear_session() plus gc.collect(); note that TensorFlow does not fully return GPU memory to the OS until the process exits — the only guaranteed way to fully free it is to end the process.
Short answer: Two different questions hide here. To stop TensorFlow grabbing all GPU memory up front, enable memory growth (set_memory_growth(gpu, True)) before any tensor is created, or cap it with set_virtual_device_configuration. To free memory already held inside a running process (e.g. training models in a loop), del the model and call tf.keras.backend.clear_session() + gc.collect() — but TensorFlow only fully returns GPU memory to the OS when the process exits.
First, separate two things people both call "clearing GPU memory":
- Preventing TensorFlow from reserving the whole GPU at startup → configure allocation (Options 1 & 2 below).
- Releasing memory a model is already holding → reset the session (the section after).
Configuring allocation
In TensorFlow 2, you configure GPU memory with the tf.config.experimental.set_memory_growth method (enable memory growth), or tf.config.experimental.set_virtual_device_configuration (a hard limit).
Let's go through both options with detailed explanations and examples:
Option 1: Enable Memory Growth By enabling memory growth, TensorFlow will allocate memory on the GPU as needed and release it when no longer in use. This allows the GPU memory to be freed up automatically. Here's how you can do it:
import tensorflow as tf
# Enable memory growth
physical_devices = tf.config.list_physical_devices('GPU')
if physical_devices:
tf.config.experimental.set_memory_growth(physical_devices[0], True)Explanation:
- First, import the
tensorflowmodule. - Use
tf.config.list_physical_devices('GPU')to get a list of available GPUs. - Check if any GPUs are available using
if physical_devices:to avoid errors. - Set memory growth to
Trueusingtf.config.experimental.set_memory_growthfor the first GPU in the list (assuming you have multiple GPUs, you can modify the index if needed).
By setting memory growth to True, TensorFlow will allocate GPU memory on an as-needed basis. This allows the memory to grow dynamically based on the requirements of your model.
Option 2: Limit GPU Memory Usage
If you want to set a specific limit on GPU memory usage, you can use tf.config.experimental.set_virtual_device_configuration. Here's an example:
import tensorflow as tf
# Limit GPU memory usage
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# Set a limit of 2GB for GPU memory
tf.config.experimental.set_virtual_device_configuration(
gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)])
except RuntimeError as e:
print(e)Explanation:
- Import the
tensorflowmodule. - Get the list of available GPUs using
tf.config.list_physical_devices('GPU'). - Check if any GPUs are available using
if gpus:. - Set the memory limit for the first GPU using
tf.config.experimental.set_virtual_device_configuration.- In the example, we limit the GPU memory to 2GB by passing
memory_limit=2048toVirtualDeviceConfiguration. - You can modify the memory limit value as per your requirements.
- If you have multiple GPUs, you can set memory limits for each of them by specifying the appropriate index and memory limit.
- In the example, we limit the GPU memory to 2GB by passing
By setting a memory limit, TensorFlow will allocate only the specified amount of GPU memory. If the model's memory requirements exceed the limit, an error will be raised.
Remember to place these code snippets before creating any TensorFlow operations or models to ensure the GPU memory configuration takes effect.
These methods provide you with the flexibility to manage GPU memory according to your needs, either by allowing memory growth or setting memory limits. Choose the option that suits your requirements best.
Releasing memory held within a process
The options above are set-once configuration. But the most common real problem is different: you build many models in a loop (hyperparameter search, cross-validation) and each one leaves memory behind until you hit an OOM. To reset TensorFlow's state between iterations:
import gc
import tensorflow as tf
for params in search_space:
model = build_model(params)
model.fit(...)
# release this iteration's graph/session state
del model
tf.keras.backend.clear_session() # clears the Keras graph and session
gc.collect() # drop lingering Python referencesclear_session() clears the current Keras graph and frees the objects tied to it, which is what lets a long loop keep running. However, TensorFlow's GPU allocator holds onto the underlying device memory for the life of the process — nvidia-smi will still show it reserved. That's expected, not a leak.
The only guaranteed full reset
If you truly need the GPU memory returned to the OS, end the process. In a notebook that means restarting the kernel; in a script, run each heavy job as a separate subprocess so exiting reclaims everything:
# run one training job per subprocess so its GPU memory is fully reclaimed on exit
import subprocess
subprocess.run(["python", "train_one.py", "--config", "a.json"])Which approach to use
| Goal | Use |
|---|---|
| Stop TF reserving the whole GPU | set_memory_growth(gpu, True) (before any op) |
| Impose a fixed memory ceiling | set_virtual_device_configuration(memory_limit=…) |
| Free state between models in a loop | del model + clear_session() + gc.collect() |
| Fully return memory to the OS | End the process (restart kernel / subprocess) |
Related guides
Sources
Key takeaways
- •TensorFlow grabs all GPU memory up front by default; memory growth makes it allocate on demand instead.
- •Enable it with tf.config.experimental.set_memory_growth(gpu, True) BEFORE any tensor or op initialises the device.
- •For a hard ceiling, use set_virtual_device_configuration with a memory_limit in MB.
- •del the model and call tf.keras.backend.clear_session() to free memory within the process.
- •TensorFlow does not return GPU memory to the OS until the process exits — expect that limit.
Frequently asked questions
How do I stop TensorFlow from taking all my GPU memory?
Enable memory growth before creating any tensors: tf.config.experimental.set_memory_growth(gpu, True). TensorFlow then allocates GPU memory on demand instead of reserving it all at startup.
Does clear_session() free GPU memory?
It clears the current graph and session state within the process, which helps when building many models in a loop, but TensorFlow keeps the underlying GPU allocation until the process exits.
Why does set_memory_growth raise an error?
It must be called before the GPU is initialised by any op. Configure it right after importing TensorFlow and before creating tensors or models.
Software Engineering Leader & Technical Author · Updated September 9, 2026