[Sqlalchemy-tickets] Issue #3931: Memory leak on creating savepoints (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
From: Bartłomiej B. <iss...@bi...> - 2017-03-06 14:05:04
|
New issue 3931: Memory leak on creating savepoints https://bitbucket.org/zzzeek/sqlalchemy/issues/3931/memory-leak-on-creating-savepoints Bartłomiej Biernacki: I found a small memory leak when using many savepoints to the same DB. Consider this code example: ``` #!python session = create_session() for _ in range(0, 5000): session.begin_nested() session.execute( sa.insert(Test).values(name='test')) session.commit() session.commit() session.close() ``` After this we can observe 5000 strings with savepoints names in memory. Diging deeply into it I found that function `sql/compiler.py:format_savepoint` saves savepoint name to `self._strings`. By default savepoint names are unique for each nested transaction so saving them for future use is redundant and each new savepoint name stays in memory. My proposition is to change the function, so savepoint name won't be saved: ``` #!python def format_savepoint(self, savepoint, name=None): value = name or savepoint.ident if self._requires_quotes(value): return self.quote_identifier(value) return value ``` Complete code example for reproducing bug in attachment. |