You should never reuse Variables indefinitely like you do with x and y. They keep track of all operations you do on them, including assignments! Every execution of the for i in range(t, maxSize): loop will make the history of x and y longer by a single assignment, so the graphs will get huge very quickly. A fix is to move the declarations of x and yt inside the inner loop like that:

model = models.resnet18(pretrained=True).cuda(0) 
for epoch in range(0,epochs): 
    correct=0
        
    for t in range(0, trainSize, batchSize):  
        idx=0 
        x_data = []
        y_data = []
        for i in range(t, maxSize): # the batch of input x and target yt are assigned by local patches of # a large image here
             x[idx,:,:,:]=image[:, i-imgW:i+imgW, i-imgW:i+imgW] 
             yt[idx]=torch.from_numpy(Class[i]) 
             x_data.append(image[:, i-imgW:i+imgW, i-imgW:i+imgW])
             y_data.append(torch.from_numpy(Class[i]))
             idx=idx+1    
        x= torch.autograd.Variable(torch.stack(x_data, 0).cuda(0))
        yt=torch.autograd.Variable(torch.cat(y_data, 0).cuda(0))
        optimizer.zero_grad()   
        output = model(x) 
        loss = criterion(m(output), yt) 
        pred = output.data.max(1)[1] 
        correct += pred.eq(yt.data).sum()
        loss.backward()
        optimizer.step()