[CONPY-263] Memory leak if cursor.close() not called explicitly Created: 2023-05-08  Updated: 2023-05-08

Status: Open
Project: MariaDB Connector/Python
Component/s: Generic
Affects Version/s: 1.1.6
Fix Version/s: None

Type: Bug Priority: Major
Reporter: G.Mech Assignee: Georg Richter
Resolution: Unresolved Votes: 1
Labels: None
Environment:

WIN10 and debian 11 bullseye


Attachments: File test.py    
Python Version: 3.10.4

 Description   

I consider this to be bug. If you think otherwise, please treat it as a feature request or update documentation.

Steps to reproduce

  1. Run mariadb server (e.g. docker run -e MARIADB_ROOT_PASSWORD="<put_password_here>" -p 3306:3306 mariadb:10.6.11 )
  2. Repeat many times (see attached script)
    1. Create connection
    2. Get cursor
    3. Use cursor (cursor.execute(…), cursor.fetchone())
    4. Close connection

Expected outcome

Memory is freed when Cursor objects get garbage collected. The documentation does not tell, that cursor.close() must be called in order to avoid memory leaks. Also PEP-249 suggests, that calling cursor.close() is optional and that the garbage collector will eventually clean up resources:

Close the connection now (rather than whenever ._del_() is called).

Actual outcome

Output of attached script:

D:\source\mariadb_memory_leak\env\lib\site-packages\mariadb\cursors.py:208: size=1133 KiB (+1133 KiB), count=30000 (+30000), average=39 B
D:\source\mariadb_memory_leak\test.py:52: size=468 KiB (+468 KiB), count=9986 (+9986), average=48 B
D:\source\mariadb_memory_leak\env\lib\site-packages\mariadb\cursors.py:317: size=78.1 KiB (+78.1 KiB), count=10000 (+10000), average=8 B

Workaround

Call cursor.close() explicitly after using a cursor.


Generated at Thu Feb 08 03:31:28 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.