Memory leak on training

I was trying to use euler based backprop through time.

for i in tqdm(range(iterations)):
    x0 = sample(batch_size)
    loss = 0.0
    for t in np.arange(start_time,end_time+1e-5,step):
        u = policy(x0)
        loss= loss+ step*cost(x0,u)
        x0 = x0 + step * dynamics(x0, u)

    del x0
    del u

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

Here sample function just samples batches from normal dist, policy is a small neural network, cost and dynamics are small functions (containing sin, cos and some small computations).

But its eating up my ram. Is there a way to clear ram after some iterations?
Thanks in advance

Could you post a minimal and executable code snippet reproducing the issue?

I found out why it was eating up my ram, i had one statement which stored the loss value into a list

losses.append(loss) 

instead of loss.item() or detaching, i put loss directly into list which ate my ram