I have a function to measure the allocated ram by python in megabytes: def getram(): print(psutil.Process(os.getpid()).memory_info().rss / 1024**2) And also I have: device = "cuda" My problem is that the following code allocates RAM and I'm going crazy because of that. Does it have an actual solution or do I have to accept my fate and switch to C++ or something? The code: getram() def load_dataset(dir, filenames): dataset = torch.zeros((len(filenames),3,256,256), device=device) getram() for i, filename in enumerate(filenames): f = read_image(f"{dir}/{filename}") if 3 != f.shape[0]: print(filename) dataset = f.to(device) getram() return dataset dataset = load_dataset(dataset_dir, dataset_filenames) getram() The code printed out the following: 533.28125 661.2890625 678.27734375 678.27734375 As you can see, as soon as I create the empty tensor with the torch.zeros(), it takes up the RAM for no reason. I tried gc.collect(), but it didn't help at all. Continue reading...