Most likely you don’t need create_graph=True as your code is also running without it. This option would be needed if you want to e.g. calculate the second derivative and I’ve also removed it.
The memory usage does not increase if you are calculating the gradients inside the loop since the computation graph is freed after each torch.autograd.grad call. I.e. the intermediate activations needed to compute the gradients are deleted. The gradients will then be accumulated into the leaf parameters and no additional tensors are needed to be kept around.