Variables hold a reference to the graph.
I presume you were keeping around Variables across the boundary of the inner for-loop, and in that case the graph will be held on across iterations of the loop. That’s why (maybe) you thought memory was leaking, but actually the Variable was remembering what was done to it across all of for idx in xrange(kpred, len_t).