我认为存在内存泄漏ndb
图书馆,但我找不到在哪里。
有没有办法避免下面描述的问题?
您是否有更准确的测试想法来找出问题所在?
这就是我重现问题的方式:
我创建了一个包含 2 个文件的简约 Google App Engine 应用程序。
app.yaml
:
application: myapplicationid
version: demo
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.*
script: main.APP
libraries:
- name: webapp2
version: latest
main.py
:
# -*- coding: utf-8 -*-
"""Memory leak demo."""
from google.appengine.ext import ndb
import webapp2
class DummyModel(ndb.Model):
content = ndb.TextProperty()
class CreatePage(webapp2.RequestHandler):
def get(self):
value = str(102**100000)
entities = (DummyModel(content=value) for _ in xrange(100))
ndb.put_multi(entities)
class MainPage(webapp2.RequestHandler):
def get(self):
"""Use of `query().iter()` was suggested here:
https://code.google.com/p/googleappengine/issues/detail?id=9610
Same result can be reproduced without decorator and a "classic"
`query().fetch()`.
"""
for _ in range(10):
for entity in DummyModel.query().iter():
pass # Do whatever you want
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
APP = webapp2.WSGIApplication([
('/', MainPage),
('/create', CreatePage),
])
我上传了该应用程序,名为/create
once.
之后,每次调用/
增加实例使用的内存。直到因错误而停止Exceeded soft private memory limit of 128 MB with 143 MB after servicing 5 requests total
.
Exemple of memory usage graph (you can see the memory growth and crashes) :
注意:可以使用其他框架重现该问题webapp2
, like web.py