You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We recently had a memory leak on one of our servers and it stopped as soon as we commented out the line of code assigning the Diagnostics attribute of an IntegrityError to a local variable within the exception handler.
The code below replicates the issue for Python 3.5.2, Psycopg 2.6.1. When I run this the amount of resident memory used by the process steadily increases, although not every time func() is called.
#!/usr/bin/env python3importpsycopg2importitertoolsimportgcimportosconn=psycopg2.connect(os.getenv('DSN'))
defmain():
withconn:
withconn.cursor() ascur:
cur.execute('CREATE TABLE IF NOT EXISTS leaky (id integer UNIQUE)')
forninitertools.count(1):
print(n, resident_memory_usage())
func()
deffunc():
withconn:
withconn.cursor() ascur:
try:
cur.execute("INSERT INTO leaky VALUES (1)")
exceptpsycopg2.IntegrityErrorase:
diag=e.diag# this assignment results in a memory leak unless it's explicitly deleted#e.diag # just accessing the attribute does not result in a memory leak#del diag # explicitly deleting stops the leak#gc.collect() # <-- garbage collecting doesn't help even though the only reference should be circulardefresident_memory_usage():
withopen('/proc/self/status') asstatus:
forlineinstatus:
parts=line.split()
ifparts[0][2:-1].lower() =='rss':
returnint(parts[1])
if__name__=='__main__':
main()
The text was updated successfully, but these errors were encountered:
Triyng to fix a reported memory leak with diags, but implementing
traversing doesn't help. Quite the opposite in the example provided in
bug #557, the leak is present even with the `del diag`.
The leak appears with Python 3.5, not with Python 2.7.
Thank you for the report and the script to reproduce, @davidcoffin !
I've played a bit with it... only to make it worse: implementing object traversal in the Diagnostics object makes only sure that the leak stays there even with the del diag :\
We recently had a memory leak on one of our servers and it stopped as soon as we commented out the line of code assigning the Diagnostics attribute of an IntegrityError to a local variable within the exception handler.
The code below replicates the issue for Python 3.5.2, Psycopg 2.6.1. When I run this the amount of resident memory used by the process steadily increases, although not every time func() is called.
The text was updated successfully, but these errors were encountered: