Generally, failing to close cursors is one of the most common kinds of memory leaks. The garbage collector can’t see the MySQL resources involved in the cursor. MySQL doesn’t know that the Python side was released unless the close()
method is called explicitly.
Rule of thumb. Open, use and close cursors in as short a span of code as you can manage.