一、安装 pynvml
pip install nvidia-ml-py3
二、查看剩余显存
"""
/home/my_name/anaconda3/envs/py-3.7-tf-1.4/bin/python
read pgu
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)# 这里的0是GPU id
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
print(meminfo.total / 1024 /1024) #第二块显卡总的显存大小
print(meminfo.used / 1024 /1024)#这里是字节bytes,所以要想得到以兆M为单位就需要除以1024**2
print(meminfo.free / 1024 /1024) #第二块显卡剩余显存大小
print(pynvml.nvmlDeviceGetCount())#显示有几块GPU
输出:
(单位是M)
11170.375
11017.6875
152.6875
4