减少Django内存使用。低挂水果? [英] Reducing Django Memory Usage. Low hanging fruit?

查看:136
本文介绍了减少Django内存使用。低挂水果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的内存使用量随着时间的推移而增加,重新启动Django对用户来说是不好的。



我不确定如何分析内存使用情况,但有一些关于如何开始测量将是有用的。



我有一种感觉,有一些简单的步骤可以产生巨大的收益。确保'调试'设置为'False'是一个明显的biggie。



有人可以建议别人吗?在低流量网站上缓存多少改进?



在这种情况下,我使用mod_python在Apache 2.x下运行。我听说mod_wsgi有点瘦,但在这个阶段切换是很棘手的,除非我知道收益会很大。



编辑:谢谢你的提示远。任何建议如何发现什么是使用记忆?有没有任何Python内存分析指南?



另外还有一些事情会使它变得很难转换到mod_wsgi,所以我想有一些想法在这个方向上向前推进之前,我可以期待的收益。



编辑: Carl在这里发布了一个稍微更详细的回复,值得一看: Django部署:切割Apache的开销



编辑: a href =http://blog.dscpl.com.au/2009/03/load-spikes-and-excessive-memory-usage.html =nofollow noreferrer> Graham Dumpleton的文章是最好我在MPM和mod_wsgi相关的东西找到。我相当失望,没有人可以提供任何有关调试应用程序本身内存使用情况的信息。



最终编辑:我已经在Webfaction上讨论了这一点,看看他们是否可以协助重新编译Apache,这是他们的话:

我真的不认为通过切换到MPM Worker + mod_wsgi设置可以获得很多好处,我估计你可以保存大约20MB,但可能不会超过那个。

所以!这让我回到了我原来的问题(我仍然没有什么更明智的)。如何确定问题在哪里?这是一个众所周知的格式,您不会在没有测试的情况下进行优化,无法查看需要优化的位置,但是在测量Python内存使用方面的教程方面几乎没有什么,并且根本没有具体针对Django。

$ b $感谢大家的帮助,但我认为这个问题仍然是开放的!



另一个最后的编辑; - ) / p>

我在django用户列表中询问了这些,并获得了一些 非常有帮助的回复



老实说最后一次更新!



刚刚发布。可能是最好的解决方案:使用Pympler对Django对象大小和内存使用情况进行分析

解决方案

确保您没有保留全局参考数据。这样可以防止python垃圾收集器释放内存。



不要使用 mod_python 。它在apache中加载一个解释器。如果您需要使用apache,请使用 mod_wsgi 代替。切换不是棘手的。这很容易 mod_wsgi 更容易配置django 比脑死亡 mod_python



如果你可以从你的要求中删除apache,那将是更好的记住你的记忆。 产卵 似乎是新的快速可扩展的方式来运行python Web应用程序。



编辑:我看不到如何切换到mod_wsgi可能是 / EM>。这应该是一个非常容易的任务。请详细说明您所使用的切换问题。


My memory usage increases over time and restarting Django is not kind to users.

I am unsure how to go about profiling the memory usage but some tips on how to start measuring would be useful.

I have a feeling that there are some simple steps that could produce big gains. Ensuring 'debug' is set to 'False' is an obvious biggie.

Can anyone suggest others? How much improvement would caching on low-traffic sites?

In this case I'm running under Apache 2.x with mod_python. I've heard mod_wsgi is a bit leaner but it would be tricky to switch at this stage unless I know the gains would be significant.

Edit: Thanks for the tips so far. Any suggestions how to discover what's using up the memory? Are there any guides to Python memory profiling?

Also as mentioned there's a few things that will make it tricky to switch to mod_wsgi so I'd like to have some idea of the gains I could expect before ploughing forwards in that direction.

Edit: Carl posted a slightly more detailed reply here that is worth reading: Django Deployment: Cutting Apache's Overhead

Edit: Graham Dumpleton's article is the best I've found on the MPM and mod_wsgi related stuff. I am rather disappointed that no-one could provide any info on debugging the memory usage in the app itself though.

Final Edit: Well I have been discussing this with Webfaction to see if they could assist with recompiling Apache and this is their word on the matter:

"I really don't think that you will get much of a benefit by switching to an MPM Worker + mod_wsgi setup. I estimate that you might be able to save around 20MB, but probably not much more than that."

So! This brings me back to my original question (which I am still none the wiser about). How does one go about identifying where the problems lies? It's a well known maxim that you don't optimize without testing to see where you need to optimize but there is very little in the way of tutorials on measuring Python memory usage and none at all specific to Django.

Thanks for everyone's assistance but I think this question is still open!

Another final edit ;-)

I asked this on the django-users list and got some very helpful replies

Honestly the last update ever!

This was just released. Could be the best solution yet: Profiling Django object size and memory usage with Pympler

解决方案

Make sure you are not keeping global references to data. That prevents the python garbage collector from releasing the memory.

Don't use mod_python. It loads an interpreter inside apache. If you need to use apache, use mod_wsgi instead. It is not tricky to switch. It is very easy. mod_wsgi is way easier to configure for django than brain-dead mod_python.

If you can remove apache from your requirements, that would be even better to your memory. spawning seems to be the new fast scalable way to run python web applications.

EDIT: I don't see how switching to mod_wsgi could be "tricky". It should be a very easy task. Please elaborate on the problem you are having with the switch.

这篇关于减少Django内存使用。低挂水果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆