当我对大量小对象(只有几个短字符串和布尔属性的 15k 个对象)运行查询时,对这些对象不做任何事情,我看到我的实例的内存使用量不断增加(增加 70Mb)。内存增加看起来与它只为查询而需要保留在内存中的数据量不成比例。
我使用的循环如下:
cursor = None
while True:
query = MyModel.all()
if cursor:
query.with_cursor(cursor)
fetched = 0
for result in query.run(batch_size = 500):
fetched += 1
# Do something with 'result' here. Actually leaving it empty for
# testing to be sure I don't retain anything myself
if fetched == 500:
cursor = query.cursor()
break
else:
break
为了确保这不是由于 appstats,我呼吁appstats.recording.dont_record()
不要记录任何统计数据。
有谁知道可能发生了什么?或者关于如何调试/分析这个的任何指针?
更新 1:我打开gc.set_debug(gc.DEBUG_STATS)
了生产代码,我看到垃圾收集器被定期调用,所以它正在尝试收集垃圾。当我gc.collect()
在循环结束时调用 a (也是请求结束);它返回0
,并没有帮助。
更新 2:我做了一些黑客攻击让 guppy 在 dev_appserver 上工作,这似乎表明,在gc.collect()
循环结束后明确指出,大部分内存被 google.appengine.datastore.entity_pb 的 'dict 。财产'。