Skip to content

Instantly share code, notes, and snippets.

@MInner
Created July 27, 2018 22:49
Show Gist options
  • Save MInner/9716950ac85b49821b56298117756451 to your computer and use it in GitHub Desktop.
Save MInner/9716950ac85b49821b56298117756451 to your computer and use it in GitHub Desktop.
Release GPU memory after tensorflow session is closed
def run_release_gpu(func):
def parallel_wrapper(output_dict, *argv, **kwargs):
ret = func(*argv, **kwargs)
if ret is not None:
output_dict['ret'] = ret
def outer_wrapper(*argv, **kwargs):
same_process = kwargs.pop('same_process', False)
if same_process:
return func(*argv, **kwargs)
with multiprocessing.Manager() as manager:
output = manager.dict()
args = (output, ) + argv
p = multiprocessing.Process(target=parallel_wrapper, args=args, kwargs=kwargs)
p.start()
p.join()
ret_val = output.get('ret', None)
return ret_val
return outer_wrapper
@run_release_gpu
def run_computations(a, b, c):
with tf.Graph().as_default():
with tf.Session() as sess:
pass
a = run_computations(1, 2, 3)
@arainboldt
Copy link

Hi, thanks for sharing this on stack overflow. Unfortunately, it still seems very relevant as a work-around, but I can't seem to get it to work as Process needs to pickle the target func, which in this case is local, and thus cannot be pickled. Did you ever get this to work?

@MInner
Copy link
Author

MInner commented Nov 20, 2020

It's being a while since I posted this, no idea whether it is still a viable workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment