Well you don’t need to retain the graph in this case I think.
But even if you do so, retain_graph only prevents it from being freed during the backward pass. But when nothing can access it anymore, it will be freed: it does not leak memory. It just delays when the memory is released.