Tf 2.x how to delete tensor

989 views
Skip to first unread message

Joeri Nicolaes

unread,
Oct 9, 2020, 10:51:01 AM10/9/20
to Discuss
Hi,

I am struggling with a simple question for Tf 2.x (running eagerly): I am loading a tensor on GPU and would like to remove it again after some calculations to free up gpu memory.

Minimal code snippet (tested with tf 2.3 and python 3.6): 

import tensorflow as tf

tf.debugging.set_log_device_placement(True)

for gpu in tf.config.experimental.list_physical_devices('GPU'):

    tf.config.experimental.set_memory_growth(gpu, True)

with tf.device('GPU:0'):

   image_shape=(500,500,500,3)

    t =tf.zeros(image_shape)

del t

I would expect the gpu memory usage to drop again but don't see any change using nvidia-smi on my system). 

I actually don't see any other way than restarting the python process that imported tensorflow to release the gpu memory again.

Can anyone educate my on how simple operation could be done in Tf 2.x? 

Thanks in advance,
Joeri

Martin Wicke

unread,
Oct 20, 2020, 1:05:56 PM10/20/20
to Joeri Nicolaes, Discuss
TensorFlow never releases GPU memory (until the process terminates), it reuses it for future allocations. It might be the case that CUDA11 has sufficiently efficient async allocation, in that case we might get garbage collection.

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/91cea488-3e14-43fa-91f6-7b341b219d6en%40tensorflow.org.
Reply all
Reply to author
Forward
0 new messages