Since this error does only occur after several iterations, it seems, that you are unintentionally keeping memory allocated.
This is often the case while logging/calculating some metrics. Could you post your code, so we can have a look at it?