# Clearing the GPU is a headache

**URL:** <https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762>\
**Category:** vision\
**Created:** [June 9, 2020, 12:49pm UTC](https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762 "2020-06-09T12:49:16Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![ljj7975](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ljj7975/32/12561_2.png) [@ljj7975](https://discuss.pytorch.org/u/ljj7975)\
**Post date:** [July 8, 2021, 3:04pm UTC](https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762/9 "2021-07-08T15:04:42Z")

</div>

is there a way to programmatically clear CUDA context?

I would like to release the memory allocated by CUDA context after model training because my process need to do other operations after that

> [@Model.to("cpu") does not release GPU memory allocated by registered buffer](https://discuss.pytorch.org/t/model-to-cpu-does-not-release-gpu-memory-allocated-by-registered-buffer/126102):
>
> I have figured that registered\_buffer does not release GPU memory when the model is moved back to CPU. Here is the minimal code for reproducing the observation import torch from torch import nn from subprocess import Popen, PIPE class TestNet(nn.Module): def \_\_init\_\_( self ): super().\_\_init\_\_() self.register\_buffer("test", torch.Tensor([1,2,3]).view(-1, 1, 1), False) def log\_nvidia\_smi(): print("--- nvidia-smi ---") for line in Popen('nvidia-smi', shel…

---

_[View the full topic](https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762)._
