How to release the memory of GPU in tensorflow?
<p>I took the CNN example from <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/docker/notebooks/3_mnist_from_scratch.ipynb" rel="nofollow noreferrer">Tensorflow</a>, and when I ran <code>prediction.eval()</code>, the GPU ran of out memory.</p>
<p>Here show the trace information</p>
<pre><code>ResourceExhaustedErrorTraceback (most recent call last)
&lt;ipython-input-23-1bdd4afd1d9a&gt; in &lt;module&gt;()
----&gt; 1 test_error, confusions = error_rate(test_prediction.eval(), test_labels)
      2 print('Test error: %.1f%%' % test_error)
      3 
      4 plt.xlabel('Actual')
      5 plt.ylabel('Predicted')

/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in eval(self, feed_dict, session)
    573 
    574     &quot;&quot;&quot;
--&gt; 575     return _eval_using_default_session(self, feed_dict, self.graph, session)
    576 
    577 

/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _eval_using_default_session(tensors, feed_dict, graph, session)
   3771                        &quot;the tensor's graph is different from the session's &quot;
   3772                        &quot;graph.&quot;)
-&gt; 3773   return session.run(tensors, feed_dict)
   3774 
   3775 

/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    764     try:
    765       result = self._run(None, fetches, feed_dict, options_ptr,
--&gt; 766                          run_metadata_ptr)
    767       if run_metadata:
    768         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    962     if final_fetches or final_targets:
    963       results = self._do_run(handle, final_targets, final_fetches,
--&gt; 964                              feed_dict_string, options, run_metadata)
    965     else:
    966       results = []

/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1012     if handle is None:
   1013       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-&gt; 1014                            target_list, options, run_metadata)
   1015     else:
   1016       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/usr/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
   1032         except KeyError:
   1033           pass
-&gt; 1034       raise type(e)(node_def, op, message)
   1035 
   1036   def _extend_graph(self):

ResourceExhaustedError: OOM when allocating tensor with shape[10000,32,28,28]
     [[Node: Conv2D_4 = Conv2D[T=DT_FLOAT, data_format=&quot;NHWC&quot;, padding=&quot;SAME&quot;, strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device=&quot;/job:localhost/replica:0/task:0/gpu:0&quot;](Const_1, Variable/read)]]

Caused by op u'Conv2D_4', defined at:
  File &quot;/usr/lib64/python2.7/runpy.py&quot;, line 162, in _run_module_as_main
    &quot;__main__&quot;, fname, loader, pkg_name)
  File &quot;/usr/lib64/python2.7/runpy.py&quot;, line 72, in _run_code
    exec code in run_globals
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/__main__.py&quot;, line 3, in &lt;module&gt;
    app.launch_new_instance()
  File &quot;/usr/lib/python2.7/site-packages/traitlets/config/application.py&quot;, line 658, in launch_instance
    app.start()
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/kernelapp.py&quot;, line 474, in start
    ioloop.IOLoop.instance().start()
  File &quot;/usr/lib64/python2.7/site-packages/zmq/eventloop/ioloop.py&quot;, line 177, in start
    super(ZMQIOLoop, self).start()
  File &quot;/usr/lib64/python2.7/site-packages/tornado/ioloop.py&quot;, line 887, in start
    handler_func(fd_obj, events)
  File &quot;/usr/lib64/python2.7/site-packages/tornado/stack_context.py&quot;, line 275, in null_wrapper
    return fn(*args, **kwargs)
  File &quot;/usr/lib64/python2.7/site-packages/zmq/eventloop/zmqstream.py&quot;, line 440, in _handle_events
    self._handle_recv()
  File &quot;/usr/lib64/python2.7/site-packages/zmq/eventloop/zmqstream.py&quot;, line 472, in _handle_recv
    self._run_callback(callback, msg)
  File &quot;/usr/lib64/python2.7/site-packages/zmq/eventloop/zmqstream.py&quot;, line 414, in _run_callback
    callback(*args, **kwargs)
  File &quot;/usr/lib64/python2.7/site-packages/tornado/stack_context.py&quot;, line 275, in null_wrapper
    return fn(*args, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/kernelbase.py&quot;, line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/kernelbase.py&quot;, line 228, in dispatch_shell
    handler(stream, idents, msg)
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/kernelbase.py&quot;, line 390, in execute_request
    user_expressions, allow_stdin)
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/ipkernel.py&quot;, line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File &quot;/usr/lib/python2.7/site-packages/ipykernel/zmqshell.py&quot;, line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File &quot;/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py&quot;, line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File &quot;/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py&quot;, line 2821, in run_ast_nodes
    if self.run_code(code, result):
  File &quot;/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py&quot;, line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File &quot;&lt;ipython-input-14-5c825de8dd77&gt;&quot;, line 31, in &lt;module&gt;
    test_prediction = tf.nn.softmax(model(test_data_node))
  File &quot;&lt;ipython-input-13-ddd4bdc4d43d&gt;&quot;, line 9, in model
    padding='SAME')
  File &quot;/usr/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py&quot;, line 396, in conv2d
    data_format=data_format, name=name)
  File &quot;/usr/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py&quot;, line 763, in apply_op
    op_def=op_def)
  File &quot;/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py&quot;, line 2371, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File &quot;/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py&quot;, line 1258, in __init__
    self._traceback = _extract_stack()

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[10000,32,28,28]
     [[Node: Conv2D_4 = Conv2D[T=DT_FLOAT, data_format=&quot;NHWC&quot;, padding=&quot;SAME&quot;, strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device=&quot;/job:localhost/replica:0/task:0/gpu:0&quot;](Const_1, Variable/read)]]
</code></pre>
<p>And before the prediction/test stage, the usage of the memory of GPU is 92%, so, at prediction stage, there is not much memory available to run <code>prediction.eval()</code>.</p>
<h3>ENV</h3>
<ul>
<li>GTX 660, 2G memory</li>
<li>tensorflow-gpu</li>
<li>8G-RAM</li>
<li>cuda-8</li>
<li>cuDNN</li>
</ul>
<p>How can I release the memory of GPU</p>