Clearing Tensorflow GPU memory after model execution
<p>I've trained 3 models and am now running code that loads each of the 3 checkpoints in sequence and runs predictions using them. I'm using the GPU.</p>

<p>When the first model is loaded it pre-allocates the entire GPU memory (which I want for working through the first batch of data). But it doesn't unload memory when it's finished. When the second model is loaded, using both <code>tf.reset_default_graph()</code> and <code>with tf.Graph().as_default()</code> the GPU memory still is fully consumed from the first model, and the second model is then starved of memory.</p>

<p>Is there a way to resolve this, other than using Python subprocesses or multiprocessing to work around the problem (the only solution I've found on via google searches)?</p>

<p>GPU memory allocated by tensors is released as soon as the tensor is not needed anymore (before the .run call terminates). GPU memory allocated for variables is released when variable containers are destroyed. In case of DirectSession (ie, sess=tf.Session("")) it is when session is closed or explicitly reset (added in  <a href="https://github.com/tensorflow/tensorflow/commit/62c159ff" rel="nofollow">62c159ff</a>)</p>

python tensorflow gpu