[Sqlalchemy-tickets] Issue #4015: Possible Memory Leak on simple operations (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
From: Luca Z. <iss...@bi...> - 2017-06-21 15:31:59
|
New issue 4015: Possible Memory Leak on simple operations https://bitbucket.org/zzzeek/sqlalchemy/issues/4015/possible-memory-leak-on-simple-operations Luca Zulian: I am currently playing around with SQLAlchemy a bit, and I found a strange behaviour about memory usage. I'm using sqlalchemy with version 1.1.10 running on python 2.7.13, macOS Sierra version 10.12.5, MySQL-python 1.2.5, mysql driver version Ver 14.14 Distrib 5.7.18, for osx10.12 (x86_64) using EditLine wrapper ``` #!python from contextlib import contextmanager import memory_profiler from sqlalchemy import create_engine, Column, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker Base = declarative_base() e = create_engine('mysql://root:root@127.0.0.1:3306/MyDb?charset=utf8') class Currency(Base): __tablename__ = "currency" id = Column(Integer, primary_key=True) Base.metadata.create_all(e) Session = sessionmaker(autoflush=True, bind=e) @contextmanager def session_scope(): """Provide a transactional scope around a series of operations.""" session = Session() try: yield session session.commit() except: session.rollback() raise finally: session.close() def foo(): with session_scope() as session2: result = session2.query(Currency).filter_by(id=1).first() print(result.id) while True: foo() ``` It seems that the memory it's never freed, and that continuously increase. Also I've got the same problem only opening and closing the session. ``` #!python from contextlib import contextmanager import memory_profiler from sqlalchemy import create_engine, Column, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker Base = declarative_base() e = create_engine('mysql://root:root@127.0.0.1:3306/MyDb?charset=utf8') class Currency(Base): __tablename__ = "currency" id = Column(Integer, primary_key=True) Base.metadata.create_all(e) Session = sessionmaker(autoflush=True, bind=e) @contextmanager def session_scope(): """Provide a transactional scope around a series of operations.""" session = Session() try: yield session session.commit() except: session.rollback() raise finally: session.close() def foo(): with session_scope() as session2: pass while True: foo() ``` |