Hi,
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
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:[email protected]: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()
@memory_profiler.profile(precision=6)
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.
The output of memory_profiler is as follow:
Line # Mem usage Increment Line Contents
================================================
40 53.875000 MiB 0.000000 MiB @memory_profiler.profile(precision=6)
41 def foo():
42 53.875000 MiB 0.000000 MiB with session_scope() as session2:
43 53.886719 MiB 0.011719 MiB result = session2.query(
Currency).filter_by(id=1).first()
44 53.886719 MiB 0.000000 MiB print(result.id)
Line # Mem usage Increment Line Contents
================================================
40 53.953125 MiB 0.000000 MiB @memory_profiler.profile(precision=6)
41 def foo():
42 53.953125 MiB 0.000000 MiB with session_scope() as session2:
43 53.957031 MiB 0.003906 MiB result = session2.query(
Currency).filter_by(id=1).first()
44 53.957031 MiB 0.000000 MiB print(result.id)
Is there a way to solve this problem? I've tried with session.expunge_all()
but it seem not working as expected. Or is there a problem with my code?
Thanks all
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.