# 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:** 4

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 11, 2020, 4:35am UTC](https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762/4 "2020-06-11T04:35:31Z")

</div>

`del tensor` should work. To release the cached memory, you would need to call `torch.cuda.empty_cache()` afterwards.

Here is a small example:

```python
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

x = torch.randn(1024*1024).cuda()

# 4MB allocation and potentially larger cache
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

y = torch.randn(8*1024*1024).cuda()

# 4+32=36MB allocation and potentially larger cache
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

del x

# 32MB allocation, cache should stay the same
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

torch.cuda.empty_cache()

# 32MB allocation and cache
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

del y

# 0MB allocation, 32MB cache
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

torch.cuda.empty_cache()

# 0MB allocation and cache
print(torch.cuda.memory_allocated()/1024**2)
print(torch.cuda.memory_cached()/1024**2)

```

---

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