Releasing RAM memory
For releasing the RAM memory, just do del Variables
as suggested by @nuric in the comment.
Releasing GPU memory
This is a little bit trickier than releasing the RAM memory. Some people will suggest you the following code (Assuming you are using keras)
from keras import backend as K
K.clear_session()
However, the above code doesn't work for all people. (Even when you try del Models
, it is still not going to work)
If the above method doesn't work for you, then try the following (You need to install the numba library first):
from numba import cuda
cuda.select_device(0)
cuda.close()
The reason behind it is: Tensorflow is just allocating memory to the GPU, while CUDA is responsible for managing the GPU memory.
If CUDA somehow refuses to release the GPU memory after you have cleared all the graph with K.clear_session()
, then you can use the cuda library to have a direct control on CUDA to clear up GPU memory.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…