In Dataloader1 cuda OOM occurs here when the tensor is created inside init(),
class DataLoader1():
    def __init__(self, args):
        ....
        self.features_vec = torch.zeros((self.num_samples, self.max_seq_len), dtype=torch.long, device=self.device)

Which is understandable, as I am trying to create a tensor of that big size. As this is in DataLoader1’s init(), I suppose the object loader is not created. I get the undefined variable error when I try
except RuntimeError as e:
    del loader

Whereas DataLoader2 looks like this;
class DataLoader2():
    def __init__(self, args):
        ...
        self.features_vec = torch.zeros((self._batch_size, self.max_seq_len), dtype=torch.long, device=self.device)

And this too raises OOM while creating self.features_vec. Here self.device = torch.device('cuda') and I can see plenty of memory left in nvidia-smi.