My guess without seeing the rest of the code is be that you are retaining loss, which in turn retains the whole history at every iteration in memory.
If you need to keep a record of loss, just make sure you’re just storing its value (on 0.4, loss.item() or float(loss); on 0.3 loss.data) rather than the whole computation graph attached to it.
Maybe it’s unrelated, hope this helps anyway