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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:17:36  来源:igfitidea点击:

Django memory usage going up with every request

pythondjangomemory-leaksmod-wsgi

提问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
  • DEBUGset to False
  • 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.adminand 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:

虽然我还没有完全解决这个问题,但这里有一些基本的提示,供其他有类似问题的人使用:

回答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

回答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。检查它没有加载。