RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94

错误:

RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94 GiB already allocated; 169.19 MiB free; 1.96 GiB reserved in total by PyTorch)

RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94

RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94

我们可以看见内存是还有很多的,但是在运行程序的时候还是会出现CUDA out of memory

解决方法

一、正确调用指定GPU

我们在调用GPU时,并没有使用到指定“内存”,使用的是显存,这样就会导致我们在跑程序的时出现内存溢出
将下面的代码添加到代码中

os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")#判断CUDA是否能使用,不可以就使用CPU

二、杀死进程

这里我们杀死进程要注意的是,杀死进程是否可以pass掉,不然会导致你有的程序终止掉,切记切记
RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94

kill id号

eg:
RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94

三、关机重启

要是前面两个方法还是没有解决问题,关机重新启动试一试。
还是不行我也表示~~~~~~sorry

希望这篇文章对你有用!
谢谢点赞评论!

上一篇:python将内存数据压缩成zip


下一篇:C++ 内存模型