I found:
import torch
import gc
for obj in gc.get_objects():
try:
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
print(type(obj), obj.size())
except: pass
Are these objects gc.get_objects()
ready to be collected or they will not be collected unless we set them to None inside of the program?
Then I found:
# prints currently alive Tensors and Variables
import torch
import gc
for obj in gc.get_objects():
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
del obj
torch.cuda.empty_cache()
What will this code do?
Is del obj
same as obj=None
.
What is the difference between torch.cuda.empty_cache()
and gc.collect()
.