python 每次请求都会增加 Django 内存使用量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2293333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django memory usage going up with every request
提问by Ludwik Trammer
I moved my first Django project from DjangoEurope to Webfaction, and that started an issue looking like a memory leak. With every single request memory usage of the server process goes up about 500kb. It never goes down. This goes on until Webfaction kills it for using too much memory.
我将我的第一个 Django 项目从 DjangoEurope 迁移到 Webfaction,这引发了一个看起来像内存泄漏的问题。服务器进程的每个请求内存使用量增加约 500kb。它永远不会下降。这种情况一直持续到 Webfaction 因使用过多内存而杀死它。
I can clearly see this when I refresh the Django's admin interface in my browser (although this happens with every single page, not only with admin interface - I though admin interface would be a nice test case, because there is no my code directly there). With every browser reload the memory usage goes up couple hundreds kilobytes.
当我在浏览器中刷新 Django 的管理界面时,我可以清楚地看到这一点(尽管这发生在每个页面上,不仅仅是管理界面 - 我虽然管理界面将是一个很好的测试用例,因为那里没有我的代码) . 每次浏览器重新加载时,内存使用量都会增加数百 KB。
I test the memory using a command suggested by Webfaction:
我使用 Webfaction 建议的命令测试内存:
ps -u publica -o rss,etime,pid,command
More about my setup:
更多关于我的设置:
- Django 1.1 (stable)
- Default Webfaction Django setup using Apache and mod_wsgi
DEBUG
set toFalse
- MySQLdb 1.2.2 from Webfaction, but after hearing it had some problems I tried version 1.2.3c. Didn't help.
- Django 1.1(稳定版)
- 使用 Apache 和 mod_wsgi 的默认 Webfaction Django 设置
DEBUG
设置False
- MySQLdb 1.2.2 来自 Webfaction,但在听说它有一些问题后,我尝试了 1.2.3c 版。没有帮助。
Edit:I created an empty Django project - default Django configuration plus django.contrib.admin
and fresh empty database (tried both with mysql and postgresql). I started reloading Django admin in my browser and watched memory usage. At first I saw the problem occurring - memory usage grew after every reload. But then it stabilized and stopped growing. That's consistant with how my original project behaved on Django Europe. Unfortunately on Webfaction it never seems to stabilize (or at least not within limits of memory available to my account). Any advice?
编辑:我创建了一个空的 Django 项目 - 默认的 Django 配置加上django.contrib.admin
和新的空数据库(用 mysql 和 postgresql 都尝试过)。我开始在浏览器中重新加载 Django admin 并观察内存使用情况。起初我看到问题发生了 - 每次重新加载后内存使用量都会增加。但随后它稳定下来并停止增长。这与我的原始项目在 Django Europe 上的表现一致。不幸的是,在 Webfaction 上,它似乎永远不会稳定(或至少不在我帐户可用的内存限制内)。有什么建议吗?
采纳答案by Ludwik Trammer
I'm afraid I haven't got any definite answers. Graham Dumpleton's tips were most helpfull, but unfortunately he didn't make an answer (just comments), so there is no way to accept his response.
恐怕我没有任何明确的答案。Graham Dumpleton 的提示最有帮助,但不幸的是他没有做出回答(只是评论),因此无法接受他的回答。
Although I still haven't fully resolved the issue, here are some basic tips for other people having similar problems:
虽然我还没有完全解决这个问题,但这里有一些基本的提示,供其他有类似问题的人使用:
- Read Webfaction's documentation:
- Make sure the
DEBUG
setting is set toFalse
- Don't use
mod_python
, usemod_wsgi
- Make sure you use the most recent version od
mod_wsgi
(Webfaction tends to install older versions) - Don't use Django to serve static content
- Try running
mod_wsgi
in a daemon mode (Webfaction installs it in embedded mode by default) [thanks Graham Dumpleton for the tip] - If you run in embeded mode, you can specify "inactivity-timeout=[seconds]" option. It will restart the process after [seconds] of inactivity, helping with increased memory usage. Read this forum post for detailed instructions.
- This scriptwill help you monitor your memory usage easier, and more precisely [thanks ClaudioA for the tip]
- 阅读 Webfaction 的文档:
- 确保
DEBUG
设置设置为False
- 不要用
mod_python
,用mod_wsgi
- 确保您使用最新版本的 od
mod_wsgi
(Webfaction 倾向于安装旧版本) - 不要使用 Django 来提供静态内容
- 尝试
mod_wsgi
在守护进程模式下运行(默认情况下,Webfaction 将其安装在嵌入式模式下)[感谢 Graham Dumpleton 的提示] - 如果您在嵌入模式下运行,您可以指定“inactivity-timeout=[seconds]”选项。它将在 [秒] 不活动后重新启动进程,有助于增加内存使用量。阅读此论坛帖子以获取详细说明。
- 此脚本将帮助您更轻松、更准确地监控内存使用情况 [感谢 ClaudioA 的提示]
回答by Emil Stenstr?m
We had a similar issue on Webfaction, but it turned out it wasn't because of them at all. There's a bug in Django about high memory usage when using the sitemap with lots of elements: http://code.djangoproject.com/ticket/11572
我们在 Webfaction 上遇到了类似的问题,但结果证明根本不是因为他们。当使用包含大量元素的站点地图时,Django 中存在一个关于高内存使用率的错误:http: //code.djangoproject.com/ticket/11572
When we removed the sitemap it doesn't shoot up 90 Mb at the time any longer. Just thought I should mention it since it took a long time for use to troubleshoot.
当我们删除站点地图时,它当时不会再增加 90 Mb。只是想我应该提到它,因为它花了很长时间来进行故障排除。
回答by Mikhail Korobov
I'd suggest not to guess.
我建议不要猜测。
Take a look at http://code.google.com/p/django-dowser/, it is a very useful app for detecting memory leaks and figuring out what parts of your code are responsible for memory consumption.
看看http://code.google.com/p/django-dowser/,它是一个非常有用的应用程序,用于检测内存泄漏并确定代码的哪些部分负责内存消耗。
回答by Ryan Ginstrom
I have the same problems with webfaction.
我有同样的问题与网络派别。
The method I use, and which webfaction told me I should keep using, is run a cron job that checks the memory every 5 minutes or so, and restarts any apps that are getting out of control.
我使用的方法是运行一个 cron 作业,每 5 分钟左右检查一次内存,并重新启动所有失控的应用程序,并且 web 告诉我应该继续使用的方法。
Out of 4 python apps on webfaction, I average 4 restarts per day.
在 webfaction 上的 4 个 python 应用程序中,我平均每天重启 4 次。
回答by Kedare
Check if the in-process memory cache backend is enabled, if yes, that could be the problem (new cache entries at each request).
检查进程内内存缓存后端是否已启用,如果是,则可能是问题所在(每个请求都有新的缓存条目)。
回答by barbara
I've had issues with memory on Webfaction too - they didn't really crop up until I added my fifth application though. I tried a few tweaks to my apache configs, but what finally worked for me was just switching over to mod_wsgi.
我也遇到了 Webfaction 上的内存问题——但直到我添加了我的第五个应用程序后,它们才真正出现。我尝试对我的 apache 配置进行一些调整,但最终对我有用的只是切换到 mod_wsgi。
回答by Claudio Acciaresi
Did you check these tips? -> http://blog.webfaction.com/tips-to-keep-your-django-mod-python-memory-usage-down
We have deploy an app in WebFaction and we are not having those issues.
Are you serving your static files as they recommend?
BTW this could be useful too: http://fightingrabbits.com/archives/198
你检查这些提示了吗?-> http://blog.webfaction.com/tips-to-keep-your-django-mod-python-memory-usage-down
我们已经在 WebFaction 中部署了一个应用程序,我们没有遇到这些问题。
您是否按照他们的建议提供静态文件?
顺便说一句,这也很有用:http: //fightingrabbits.com/archives/198
回答by John Mee
Are both mod_python and mod_wsgi modules loaded into apache?
mod_python 和 mod_wsgi 模块都加载到 apache 中了吗?
I know mod_wsgi doesn't like having mod_python in its building. Check its not loaded.
我知道 mod_wsgi 不喜欢在其建筑物中使用 mod_python。检查它没有加载。