I made a small tkinter
game that uses turtle
for graphics. It's a simulation of the Triangle Peg Game from Cracker Barrel that is able to tell the player the next best move to make at any point in the game, among other features. Pegs are just instances of a subclass of turtle.RawPen
, and I keep plenty of plain instances of RawPen
around to draw arrows representing moves.
I noticed that when I restart the game (which calls turtle.bye()
) to kill the turtle window, that memory consumption actually increases, as turtles don't seem to be deleted. Even if I call window.clear()
beforehand, which clears _turtles
in window.__dict__
, there are still references to the turtles. I ensured that all the references that I make to them are deleted during restart, so that's not the issue. Is there any way to truly delete a turtle so it can be garbage collected?
window.clear()
thenwindow.bye()
to get individual turtle reference counts to drop to zero. The.clear()
takes care ofwindow._turtles
and the.bye()
takes care of extra references caused by turtle event methods likeonclick()
. Can't say where your additional references are coming from. – Uroscopygc.get_referrers()
during debugging on all turtles used (I store them inpeg_dir
,graveyard
, andartist_dir
) and the only references were the list containing them,window._turtles
, and event methods. I've tried clearing all my lists, usingwindow.clear()
, and thenwindow.bye()
, andgc.collect()
says that the objects inpeg_dir
are unreachable. No idea why. I'll look into it. – Browse