假设我创建了一个张量并将其放在GPU上,以后不再需要,并且想要释放分配给它的GPU内存;我该怎么办?
import torch
a=torch.randn(3,4).cuda() # nvidia-smi shows that some mem has been allocated.
# do something
# a does not exist and nvidia-smi shows that mem has been freed.
我尝试过:
del a
del a; torch.cuda.empty_cache()
但是它们都不起作用。
答案 0 :(得分:1)
运行del tensor
会从GPU释放内存,但不会将其返回给设备,因为该内存在nvidia-smi
上显示为未释放。您可以创建一个新的张量,并将重用该内存。
https://discuss.pytorch.org/t/how-to-delete-pytorch-objects-correctly-from-memory/947 https://discuss.pytorch.org/t/about-torch-cuda-empty-cache/34232