使用mysql-python时,内存泄漏大数据集

时间:2013-06-14 14:20:57

标签: python mysql

我在使用MySQLdb API时遇到了我认为是内存泄漏的问题

Line #    Mem usage    Increment   Line Contents
================================================
     6                             @profile
     7    10.102 MB     0.000 MB   def main():
     8    10.105 MB     0.004 MB       connection = MySQLdb.connect(host="localhost", db="mydb",
     9    11.285 MB     1.180 MB                                    user="notroot", passwd="Admin123", use_unicode=True)
    10    11.285 MB     0.000 MB       cursor = connection.cursor(cursorclass=MySQLdb.cursors.SSCursor)
    11                                 
    12    11.289 MB     0.004 MB       cursor.execute("select * from a big table;")
    13                                 
    14   254.078 MB   242.789 MB       results = [result for result in cursor]
    15   251.672 MB    -2.406 MB       del results
    16   251.672 MB     0.000 MB       return

当使用guppy / hpy探索堆时,它表明我的大部分内存都被unicode对象,int和datetime对象占用(很可能是由MySQLdb API返回的行)

我在Ubuntu 12.04上使用Python 2.7.3,mysql-python==1.2.4,并使用memory_profiler进行了分析。

这可能是http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-delete-a-large-object.htm

中描述的实习生

我错过了悬挂的任何引用吗?

编辑:我也关闭了游标和连接但仍然得到了类似的结果。

解决: 捂脸。我正在做一个列表理解,自然地将所有内容保存在内存中。正确使用迭代器(流式传输到文件或其他东西)时,它具有相当的内存使用率。

Line #    Mem usage    Increment   Line Contents
================================================
    16                             @profile
    17    10.055 MB     0.000 MB   def main():
    18    10.059 MB     0.004 MB       connection = MySQLdb.connect(host="localhost", db="mydb",
    19    11.242 MB     1.184 MB                                    user="notroot", passwd="Admin123", use_unicode=True)
    20    11.242 MB     0.000 MB       cursor = connection.cursor(cursorclass=MySQLdb.cursors.SSCursor)
    21                                 
    22    11.246 MB     0.004 MB       cursor.execute("select * from big table")
    23    11.246 MB     0.000 MB       count = 0
    24    30.887 MB    19.641 MB       for result in cursor:
    25    30.887 MB     0.000 MB           count = count + 1
    26    30.895 MB     0.008 MB       cursor.close()
    27    30.898 MB     0.004 MB       connection.close()
    28    30.898 MB     0.000 MB       return

1 个答案:

答案 0 :(得分:1)

由OP解决。他的原始代码包含

results = [result for result in cursor]

此列表理解将整个结果存储在内存中,而不是根据需要从服务器中流式传输。 OP用简单的

代替了它
for result in cursor:
    ...

并看到他的记忆力恢复正常。

相关问题