Django高内存使用
我将Django用作React前端的后端,并在Heroku上部署两个应用程序。
我还使用Gunicorn为应用程序服务,并在Heroku上签署了业余计划,该计划提供了512 MB的RAM以供运行。
但是Django Dyno几乎总是在使用大量内存,并且超过了512 MB的限制。每当我重新启动或部署更改时,它只能降低到40 MB的使用情况,但是一旦任何用户使用该系统并调用了一些查询。记忆越来越大。
我已经阅读了一些几天的Django和Django-Rest-Framework Memory优化,并尝试了一些更改:使用- preload
在Gunicorn上,设置-Max-Requests 要在存储器上太重时杀死它们,我还设置了数据库的
如下所述:conn_max_age
,web_concurrency
> https:// devcenter.heroku.com/articles/python-concurrency-and-database-connections
,
但这些都没有给我足够的结果。我猜现在是错误的是我的查询,因为我已经看到了一些有关.Iterator()
在查询中的用法的文章,以及它如何防止de Queries被应用程序缓存。在我的任何查询中都没有使用它。
我认为缓存查询根本不会对我的应用程序有所帮助,我什至将一些结果存储在React状态上,以防止查询再次被调用。
我尝试在某些查询上使用.iterator()
,但我注意到,当内存上升到容器上时,它持续了很长时间。我看到消耗保持不变长达6个小时,而且我不认为记忆中的查询缓存会持续很长时间(或者会吗?)。
因此,现在我对接下来要做什么以及我应该集中的事情和任何帮助的人感到有些困惑。提前致谢!
edit
刚刚附加了一个图像,该图像显示了60 MB的内存使用量仅是因为我称之为注销功能!对我来说毫无意义...而且在上升之后,需要很长时间才能再次回落。 print_from_heroku_logs
I'm using django as a backend for a React frontend, and deploying both applications on Heroku.
I also use Gunicorn do serve the application and signed the Hobby plan on Heroku which offers 512 MB of RAM for the application to run.
But the django dyno, is almost always using a lot of memory, and exceeding the 512 mb limit. It goes down to only 40 MB of usage whenever I restart or deploy changes, but as soon as any user uses the system and calls some queries. The memory goes up a lot.
I've read about django and django-rest-framework memory optimization for some days now, and tried some changes like: using --preload
on Gunicorn, setting --max-requests
to kill process when they're too heavy on memory, I've also set CONN_MAX_AGE
for the database and WEB_CONCURRENCY
as stated on:
https://devcenter.heroku.com/articles/python-concurrency-and-database-connections
But none of that gave me a good enough result. What I'm guessing is wrong now are my queries, because I've seen some articles about the usage of .iterator()
on queries and how it prevents de queries from being cached by the application and I didn't use it in any of my queries.
I don't think caching the queries would help on my application at all, I even store some of the results on React state exactly to keep the queries from being called again.
I tried using .iterator()
on some queries but I noticed that when the memory goes up on the container it stays up for a very long time. I saw consumption remain the same for up to 6 hours straight, and I don't think that a query cache would be maintained in memory for so long (or would it?).
So, now I'm a little confused about what to try next and on what I should focus and any help is welcome. Thanks in advance!
EDIT
Just attached an image which shows the memory usage going up 60 MB only because I called the logout function!! Makes no sense to me... Also after it goes up it takes a really, really long time to go back down again.
PRINT_FROM_HEROKU_LOGS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用Memroy Profiler来查看什么函数或方法分配内存
一个示例工具是 memray ,安装了它后,按照此操作运行DJANGO服务器,
请访问页面或访问页面 :可能使用大量内存的API然后结束程序运行(在Linux使用CTRL+C上)
它将生成带有内存使用详细信息的文件,并向您展示如何将其转换为可读格式,您可以在此处粘贴在此处获得一些见解如果你不能自己阅读
You have to use a memroy profiler to see what function or method allocate memory
An example tool is memray, after installing it, run the django server like this:
Visit the pages or call the APIs that might use a lot of memory then end the program run (on linux use CTRL+c)
It will generate a file with memory usage details and show you how to convert it to readable format, you can paste here to get some insights if you can't read it by yourself