显存查看
nvidia-smi:查看显存
torch.cuda.empty_cache() :不改变显存,只是帮助 nvidia-smi 看见pytorch自动释放后的显存。
内存自动回收
引用计数
按照GC的原理,python采用“引用计数”管理内存。当局部变量不存在引用的时候(引用计数为0),会自动回收内存。计算规则见博客。
import sys
input = 1
sys.getrefcount(input) # 查看引用个数
删除引用
del
删除变量对数据的引用
导致显存增加的例子
- https://www.jianshu.com/p/b43d0c2fe04f
- https://blog.csdn.net/FrontierSetter/article/details/86548042
OOM时采取的操作
参考博客:https://www.zhihu.com/question/274635237
nn.ReLU(inplace=True)
x = self.conv(x)
代替引入新变量del
某些变量(只在某些情况下有用,还未总结出结论)checkpoint