16

My server configuration is as follows:

  1. apache 2.4.23.
  2. Mod_wsgi 4.5.9

By using the Django framework and apache server, we call the Keras deep learning model. And after the successful calling of the model, the model has been always running in the GPU memory, which causes the GPU memory can not be released except by shutting down the apache server.

So, is there any way to control the release of GPU memory when calling a Keras model by Apache+Mod_wsgi+Django?

Thanks!

Runtime memory footprint screenshots

3 Answers 3

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.

4
  • This one actually worked for me when using Keras with TensorFlow backend in a Jupyter Notebook.
    – sunside
    Commented Oct 12, 2018 at 19:01
  • 2
    Thanks! No other method works for me. But is there way to run new Keras model without Jupyter Kernel restart? Commented Jan 26, 2019 at 13:14
  • 10
    Once i have closed cuda using cuda.close(), how do i train new model
    – Nitin
    Commented Jul 16, 2019 at 13:24
  • 2
    When trying to train new model, kernel dead happens
    – MCPMH
    Commented May 21, 2022 at 1:53
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.

1
from numba import cuda
device = cuda.get_current_device()
device.reset()
1
  • Code only answers are considered low quality. Without sufficient explaination, your answer is hard to understand. If the OP can't understand your answer, then he also won't be able to reproduce your possible solution. As such it would be worthless to him/her. Please add a sufficient explaination of your possible solution.
    – tacoshy
    Commented May 21, 2022 at 3:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.