Seeing memory used by each variable in Python / PyTorch?

Hi Gang!

I’m working with large 3D MR images and I’m making 3D tensors out of them. These tensors take a lot of memory, and I want to optimize my algorithm to minimize memory usage.

Is there any way to see how much memory each tensor is occupying? I tried sys.getsizeof(tensor), but it doesn’t show the memory usage by the tensor.

I would appreciate any help!

Assuming you would like to check the device memory used to store the tensor data, you could multiply the tensor.nelement() with the number of bytes per element (4 for the default float32 dtype) and would get the actual size.

1 Like

Thank you so much :slight_smile: