[Fixed]-How to release the occupied GPU memory when calling keras model by Apache mod_wsgi django?

15👍

from keras import backend as K
K.clear_session()

This will clear the current session (Graph) and so the stale model should be removed from GPU. If it didn’t work, you might need to ‘del model’ and reload it again.

👤orabis

19👍

For people who fail to make K.clear_session() work, there is an alternative solution:

from numba import cuda
cuda.select_device(0)
cuda.close()

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 GPU memory.

0👍

from numba import cuda
device = cuda.get_current_device()
device.reset()
👤MCPMH

Leave a comment