我在使用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
Run Code Online (Sandbox Code Playgroud)
此外,当使用guppy/ 探索堆时,hpy它显示我的大部分内存都被unicode对象,int和datetime对象占用(很可能是由MySQLdb API返回的行).
我mysql-python==1.2.4在Ubuntu 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中描述的实习生吗?
我错过了悬挂的任何引用吗?
编辑:我也关闭了光标和连接,但仍然得到了类似的结果.
解决: Facepalm.我正在做一个列表理解,自然地将所有内容保存在内存中.正确使用迭代器(流式传输到文件或其他东西)时,它具有不错的内存使用率.
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
Run Code Online (Sandbox Code Playgroud)
由OP解决。他的原始代码包含以下行
results = [result for result in cursor]
Run Code Online (Sandbox Code Playgroud)
此列表理解将整个结果存储在内存中,而不是根据需要从服务器流式传输。OP用一个简单的替换了它
for result in cursor:
...
Run Code Online (Sandbox Code Playgroud)
并看到他的内存使用情况恢复正常。
| 归档时间: |
|
| 查看次数: |
1993 次 |
| 最近记录: |