Search code examples
pythonmemory-leakssqlobject

sqlobject: leak memory using selectby method


I had discovered that using selectby method with sqlobject there are leak memory.

For example, when I execute this code:

connection = connectionForURI('postgresql://test:test@localhost/test_sql_object')
tran = connection.transaction()
sqlhub.threadConnection = tran


class Person(SQLObject):

    firstName = StringCol()
    middleInitial = StringCol(length=1, default=None)
    lastName = StringCol()


for iteration in range(20):
    start = datetime.now()
    for key in range(1000):
        person = Person.selectBy(firstName='name%s%s' % (iteration,key))
        if person:
             select = person[0]  
    end = datetime.now()
   print "TIME ",iteration,':' , end - start

and the result is

TIME  0 : 0:00:03.328198
TIME  1 : 0:00:03.382905
TIME  2 : 0:00:03.690991
TIME  3 : 0:00:04.436301
TIME  4 : 0:00:05.021656
TIME  5 : 0:00:05.393993
TIME  6 : 0:00:05.791572
TIME  7 : 0:00:06.151833
TIME  8 : 0:00:06.517327
TIME  9 : 0:00:06.779239
TIME  10 : 0:00:06.961454
TIME  11 : 0:00:06.872361
TIME  12 : 0:00:07.114973
TIME  13 : 0:00:07.473208
TIME  14 : 0:00:07.737618
TIME  15 : 0:00:07.951056
TIME  16 : 0:00:08.199360
TIME  17 : 0:00:08.600283
TIME  18 : 0:00:08.802639
TIME  19 : 0:00:09.131514s

I test use connection.clear(), expire, etc but I get the same result. For example, if I change Person class

class Person(SQLObject):
    class sqlmeta:
        cacheValues = False

    firstName = StringCol()
    middleInitial = StringCol(length=1, default=None)
    lastName = StringCol()

The result is

TIME  0 : 0:00:03.298583
TIME  1 : 0:00:03.465153
TIME  2 : 0:00:03.955067
TIME  3 : 0:00:04.594931
TIME  4 : 0:00:05.062099
TIME  5 : 0:00:05.172968
TIME  6 : 0:00:06.011087
TIME  7 : 0:00:06.106087
TIME  8 : 0:00:06.475573
TIME  9 : 0:00:06.836605
TIME  10 : 0:00:06.963226
TIME  11 : 0:00:06.889263
TIME  12 : 0:00:07.219188
TIME  13 : 0:00:07.417601
TIME  14 : 0:00:07.737096
TIME  15 : 0:00:08.128259
TIME  16 : 0:00:08.217653
TIME  17 : 0:00:08.612468
TIME  18 : 0:00:08.800511
TIME  19 : 0:00:08.979550

Thanks.


Solution

  • If you run the test with profile, for example python -m cProfile -s cumulative You can check that the problem is in the cursor of psycopg2.

    The solution is in https://sourceforge.net/tracker/?func=detail&atid=540673&aid=3366899&group_id=74338