10

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 によって返される行である可能性が非常に高い)。

私はmysql-python==1.2.4Ubuntu 12.04 でPython 2.7.3 を使用しており、 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
4

1 に答える 1