I'm currently struggling to find out how much memory I actually have left on my device after I have deleted an instance of a class. I have tried to use the library psutil
but it doesn't correctly show the memory I actually have available. In particular:
import psutil
x = torch.randint(0,255,size=(300,3,200,200),dtype=torch.float32)
conv = Modules.Conv2D(3,3,2,10,save_flag=False)
print("Used memory:", psutil.virtual_memory().percent,"% Free memory:", round(psutil.virtual_memory().available * 100 /
psutil.virtual_memory().total),"%")
#Used memory: 74.0% Free memory: 26.0 %
conv
is an object of my custom class and It has different attributes that take up a lot of memory(as you can see from psutil). Now If I try the following:
import gc
del conv
gc.collect()
print("Used memory:", psutil.virtual_memory().percent,"% Free memory:", round(psutil.virtual_memory().available * 100 /
psutil.virtual_memory().total),"%")
#Used memory: 74.0% Free memory: 26.0 %
Even if I delete the instance and I explicitly call python's garbage collector, the memory doesn't seem to be freed up. Although if I check with Windows' resource manager the memory is actually emptied:
#Before executing the program --> In use: 6025 MB
exec_program()
#Program is executing and conv instance is allocated --> In use: 6899 Mb
# gc.collect is called --> In use: 6058 Mb
end_of_program()
So the problem is that psutil doesn't represent correctly this situation. My guess is that although the memory is freed up by gc.collect
the virtual memory address is still marked as used even if it is actually empty, hence the misrepresentation. Is there any other way/library that can do correctly what I'm trying to achieve?
Thanks in advance!