Django为什么不发布获取的db对象? [英] Why does not Django release fetched db objects?
问题描述
我有一个非常简单的django应用程序:
I have very simple django app:
models.py:
models.py:
class Product(models.Model):
name = models.CharField(max_length=1000, default="")
desc = models.TextField(default="")
views.py:
from django.http import HttpResponse
from models import Product
def fetch(request):
for p in Product.objects.all()[:300000]:
pass
return HttpResponse("done")
我已经在MySQL数据库中加载了300k样本记录,在settings.py
中关闭了调试功能,并尝试执行了fetch
视图-完成后,django仍然位于700Mb的RAM上
I've loaded 300k sample records in MySQL database, turned debug off in settings.py
and tried executed fetch
view - after it completes, django still sits on 700Mb of RAM
我知道它需要内存来提取所有这30万个对象,但是为什么在视图函数退出后,它会保留它们呢?
I understand that it needs memory to fetch all these 300k objects, but why on earth it keeps them after view functions exits?
同样,我使用DEBUG=False
,并在django dev Web服务器以及uwsgi
及其相同的怪异行为中进行了尝试.
Again, I'm with DEBUG=False
, tried this with django dev web server and also with uwsgi
and its the same weird behavior.
P.S.在python2.6/2.7 Linux 64位系统上都使用Django 1.4和1.5.4进行了验证
P.S. Verified with Django 1.4 and 1.5.4 on both python2.6/2.7 Linux 64-bit
推荐答案
这与Django无关.通常,Python直到需要时才将内存返回给操作系统.
This is not really anything to do with Django. Generally, Python does not return memory to the operating system until it needs to.
请参见 effbot的解释以获取更多详细信息.
See the effbot's explanation for more detail.
这篇关于Django为什么不发布获取的db对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!