# Is there a way to release gpu memory held by cuda tensors/variables?

**URL:** <https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377>\
**Category:** Uncategorized\
**Created:** [June 27, 2017, 10:11pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377 "2017-06-27T22:11:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexbellgrande](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alexbellgrande/32/163_2.png) [@alexbellgrande](https://discuss.pytorch.org/u/alexbellgrande)\
**Post date:** [June 27, 2017, 10:11pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/1 "2017-06-27T22:11:24Z")

</div>

I’m trying to run a pytorch script in ipython so that I can monkey around with the outputs of my computation graph. The problem that my gpu runs out of memory after I run the same script a few times, forcing me to exit and reenter ipython. In nvidia smi you can see the gpu memory decrease by the same amount after every run until it eventually reaches 0 and throws an error. Pytorch seems to be allocating new gpu memory every time the script is executed instead of reusing the memory allocated in previous runs. Is there a way to forcibly release all gpu memory held by pytorch in between script executions so that I don’t have to constantly exit and reenter ipython? Thanks!

I’ve tried %reset and unfortunately this doesn’t do the trick (not unexpectedly).

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [July 3, 2017, 12:31am UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/2 "2017-07-03T00:31:01Z")

</div>

We haven’t exposed this functionality of flushing memory at the python level yet.

---

<div class="post-metadata">

**Author:** ![jef](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jef/32/2233_2.png) [@jef](https://discuss.pytorch.org/u/jef)\
**Post date:** [November 14, 2017, 4:05pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/3 "2017-11-14T16:05:58Z")

</div>

I want it too. Hope this task is running.

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [November 14, 2017, 4:23pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/4 "2017-11-14T16:23:53Z")

</div>

The method is available now [http://pytorch.org/docs/master/cuda.html#torch.cuda.empty\_cache](http://pytorch.org/docs/master/cuda.html#torch.cuda.empty_cache). Note that you need to delete all references to the variable you want to free before calling this. This requirement is due to safety concern. @jef

---

<div class="post-metadata">

**Author:** ![jmaronas](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jmaronas/32/2816_2.png) [@jmaronas](https://discuss.pytorch.org/u/jmaronas)\
**Post date:** [November 23, 2017, 3:10pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/5 "2017-11-23T15:10:50Z")

</div>

Well. At least in my pytorch version is not implemented

> > > import torch  
> > > a=torch.cuda.FloatTensor(10,10)  
> > > del a  
> > > torch.cuda.empty\_cache()  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > AttributeError: ‘module’ object has no attribute ‘empty\_cache’

I have just installed the last pytorch version from the webpage. Python2.7 cuda8 and pip installation.

I think this should be quickly fixed as I am getting out of memory when running on a 8GB nvidia gpu. I have two networks sharing some parameters. The problem is I get out of memory after some batches… this does not make a lot of sense however as each batch I do same operation. Forward for network 1, forward for network 2 and backward. Each data from the data iterator is copied into a Variable so I can use the nn.Module, but I think this should not allocate more memory. After all the batches nvidia-smi does not show more allocation.

Thanks.

---

<div class="post-metadata">

**Author:** ![SimonW](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simonw/32/1702_2.png) [@SimonW](https://discuss.pytorch.org/u/SimonW)\
**Post date:** [November 23, 2017, 9:30pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/6 "2017-11-23T21:30:40Z")

</div>

Pip install is only the 0.2 version. If you look at the doc, the method is only available at \>= 0.3. You will need to either build from source or wait for 0.3 release.

---

<div class="post-metadata">

**Author:** ![KeithYin](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/keithyin/32/1668_2.png) [@KeithYin](https://discuss.pytorch.org/u/KeithYin)\
**Post date:** [November 29, 2017, 4:00pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/8 "2017-11-29T16:00:55Z")

</div>

this function is very useful

---

<div class="post-metadata">

**Author:** ![lbdalmendrayCaseguar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lbdalmendraycaseguar/32/32221_2.png) [@lbdalmendrayCaseguar](https://discuss.pytorch.org/u/lbdalmendrayCaseguar)\
**Post date:** [January 8, 2021, 9:59pm UTC](https://discuss.pytorch.org/t/is-there-a-way-to-release-gpu-memory-held-by-cuda-tensors-variables/4377/9 "2021-01-08T21:59:38Z")

</div>

Hi ,  
I read your message. I would like to know if the exposed functionality of flushing memory is for C++ Libtorch developers . I am using Libtorch C++ and I cannot find a way to release ALL the CUDA GPU Memory used by a torch::nn::Module . Here I explained better an example : [https://discuss.pytorch.org/t/release-all-cuda-gpu-memory-using-libtorch-c/108303](https://discuss.pytorch.org/t/release-all-cuda-gpu-memory-using-libtorch-c/108303)

Thanks in advance .
