如果在python內調用pytorch有可能顯存和GPU占用不會被自動釋放,此時需要加入如下代碼
torch.cuda.empty_cache()
我們來看一下官方文檔的說明
Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU application and visible in nvidia-smi.
Note
empty_cache() doesn't increase the amount of GPU memory available for PyTorch. See Memory management for more details about GPU memory management.
此外還可以使用
memory_allocated()和max_memory_allocated()
觀察顯存占用,并使用
memory_cached()和 max_memory_cached()
觀察由緩存分配器管理的內存。
以上這篇Pytorch釋放顯存占用方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/jishuqianjin/article/details/89406205