Line # Mem usage Increment Line Contents
================================================
223 144.2 MiB 144.2 MiB @profile(stream=fp)
224 def compute():
226 155.4 MiB 11.2 MiB obj= session.query(Table).filter(Table.idTable == some_id).scalar()
589 161.4 MiB 0.0 MiB session.flush(obj)
590 161.4 MiB 0.0 MiB session.expunge_all()
591 161.4 MiB 0.0 MiB session.close()
592 161.4 MiB 0.0 MiB s.close_all_sessions()
593 161.4 MiB 0.0 MiB obj= None
594 161.4 MiB 0.0 MiB gc.collect()
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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 sqlalchemy+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/94afa0c9-7b4b-432c-8c8a-709ae366522f%40googlegroups.com.
import gc
import sqlalchemy.orm.session as s
from MyDatabase.model.Table import Table
from memory_profiler import profile
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
HOST = 'myhost'
engine = create_engine(
'postgresql+psycopg2://postgres:myaccount@{}:5432'.format(HOST),
connect_args={'connect_timeout': 6000})
Session = sessionmaker(bind=engine)
session = Session() # type: Session
fp = open('memory_profiler_elem.log', 'w+')
@profile(stream=fp)
def calculate():
obj= session.query(Table).filter(Table.idTable == 1).scalar()
session.flush(obj)
session.expunge_all()
session.close()
s.close_all_sessions()
obj= None
gc.collect()
if __name__ == '__main__':
calculate()
Line # Mem usage Increment Line Contents
================================================
19 140.8 MiB 140.8 MiB @profile(stream=fp)
20 def calculate():
21 154.2 MiB 13.4 MiB obj= session.query(Table).filter(Table.idTable== 1).scalar()
22 154.2 MiB 0.0 MiB session.flush(obj)
23 154.2 MiB 0.0 MiB session.expunge_all()
24 154.2 MiB 0.0 MiB session.close()
25 154.2 MiB 0.0 MiB s.close_all_sessions()
26 154.2 MiB 0.0 MiB obj= None
27 154.2 MiB 0.0 MiB gc.collect()
from memory_profiler import profile
fp = open('memory_profiler_elem.log', 'w+')
@profile(stream=fp)
def calculate():
test = []
for i in range(1000000):
test.append(1)
test = None
if __name__ == '__main__':
calculate()
Line # Mem usage Increment Line Contents
================================================
4 51.2 MiB 51.2 MiB @profile(stream=fp)
5 def calculate():
6 51.2 MiB 0.0 MiB test = []
7 58.9 MiB 0.0 MiB for i in range(1000000):
8 58.9 MiB 0.8 MiB test.append(1)
9 51.3 MiB 0.0 MiB test = None
I'll try to recreate the issue for you to test it, but my question really is : Is there something I didn't get about memory usage in sqlalchemy, meaning, is it normal for it to retain objects in memory, and if so, how can we get past it ?
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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 sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/be105ac2-e0e0-4057-a0aa-fe071adf3e6d%40googlegroups.com.
def strong_reference_session(session):
@event.listens_for(session, "pending_to_persistent")
@event.listens_for(session, "deleted_to_persistent")
@event.listens_for(session, "detached_to_persistent")
@event.listens_for(session, "loaded_as_persistent")
def strong_ref_object(sess, instance):
if 'refs' not in sess.info:
sess.info['refs'] = refs = set()
else:
refs = sess.info['refs']
refs.add(instance)
I tried strong referencing the objects stored in the session using :def strong_reference_session(session):
@event.listens_for(session, "pending_to_persistent")
@event.listens_for(session, "deleted_to_persistent")
@event.listens_for(session, "detached_to_persistent")
@event.listens_for(session, "loaded_as_persistent")
def strong_ref_object(sess, instance):
if 'refs' not in sess.info:
sess.info['refs'] = refs = set()
else:
refs = sess.info['refs']
refs.add(instance)as specified in the docs.
Still, I have no way to get control over my object, and in the example I sent you I stay in the same scope, so session shouldn't lose reference to the object anyhow.
Moreover, in this example, I am just requesting the object, I don't make any modifications on it, yet I can't flush, expire or expunge it, and finally, the session.close() don't free the memory.I don't get why in the simple example I sent you I can't flush my object. If i was to loop over this function, my memory would grow infinitely, and that's precisely what's happening when my script is running.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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 sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/99190797-eeb3-4ca1-9fd4-adde124ae3b5%40googlegroups.com.