Excessive memory usage with uwsgi/nginx

2,015 views
Skip to first unread message

Lamps902

unread,
Jun 3, 2013, 9:45:58 AM6/3/13
to web...@googlegroups.com
My web2py application is running on Webfaction's servers over uwsgi/nginx. When the servers are started, four instances of nginx and four instances of uwsgi are spawned, with each nginx instance taking up ~18M of RAM, and each nginx instance taking up a couple of M. Accessing any part of the page (i.e. clicking a menu item) adds about 30M to one of the uwsgi instances. Clicking a menu item again can add between 2 and 30 M to any of the uwsgi instances (it will be 30 M if nothing has yet been added to the RAM usage of that particular instance). This happens until my memory limit (256 M) is exceeded. I think I've mitigated the behavior somewhat by moving some things from models to modules, and by serving static content separately, but I think my memory usage is still quite excessive. Is this normal behavior?

Should I try to move all databases from models to modules? Is it ok to have globals (i.e. settings = Storage()) declared in models? Is it alright to have import statements in models? In the controllers, is it better to have import statements inside or outside of functions?

As far as uwsgi goes, would it make sense to use the 'reload-on-rss' directive to restart when excessive memory is used? Are there any major downsides to this? Any other tips for optimizing uwsgi?

Any other tips for diagnosing the sources of excessive memory consumption, and diminishing this consumption? Thank you.

Roberto De Ioris

unread,
Jun 3, 2013, 10:32:10 AM6/3/13
to web...@googlegroups.com

> My web2py application is running on Webfaction's servers over uwsgi/nginx.
> When the servers are started, four instances of nginx and four instances
> of
> uwsgi are spawned, with each nginx instance taking up ~18M of RAM, and
> each
> nginx instance taking up a couple of M. Accessing any part of the page
> (i.e. clicking a menu item) adds about 30M to one of the uwsgi instances.
> Clicking a menu item again can add between 2 and 30 M to any of the uwsgi
> instances (it will be 30 M if nothing has yet been added to the RAM usage
> of that particular instance). This happens until my memory limit (256 M)
> is
> exceeded. I think I've mitigated the behavior somewhat by moving some
> things from models to modules, and by serving static content separately,
> but I think my memory usage is still quite excessive. Is this normal
> behavior?
>

30-40 MB per worker is pretty normal.

To gain memory you can move to multithreading:

master = true
processes = 2
threads = 2
thread-stacksize = 512

should be good enough

Monitoring memory is a good thing, use --reload-on-rss 80 to avoid your
app growing up

--
Roberto De Ioris
http://unbit.it

Lamps902

unread,
Jun 3, 2013, 11:04:25 AM6/3/13
to web...@googlegroups.com
Thanks, Roberto. It was eventually growing to something in excess of 80M per uwsgi instance, then crashing when all are full. Will try the settings you suggested.

Massimo Di Pierro

unread,
Jun 3, 2013, 12:55:02 PM6/3/13
to web...@googlegroups.com
Mind that caching too much stuff will do that to you.

Lamps902

unread,
Jun 3, 2013, 1:55:28 PM6/3/13
to web...@googlegroups.com
Hi, Massimo. I didn't cache much at first, and to make sure that caching isn't the source of the problem, I tried removing all caching. No noticeable improvement there. Right now, I'm focused on moving a lot of the model db and global var/settings stuff to modules (but I find this a bit perplexing, as it seems to undermine the whole notion of the MVC framework).

Anthony

unread,
Jun 3, 2013, 2:21:03 PM6/3/13
to web...@googlegroups.com
Are you saying every single request adds 2M to 30M to the memory consumed, and it just keeps growing with every request?

Lamps902

unread,
Jun 3, 2013, 2:40:19 PM6/3/13
to
Initially, yes - every time I clicked on something that requires interaction with the server, it would add 2-30M. I don't know if there was a limit to how much memory would ultimately be consumed, because I would reach my memory limit, and Webfaction would kill my nginx/uwsgi processes. After moving a bunch of stuff to models, I get the impression that it has improved, and that the memory consumption does seem to taper off after I've clicked around for a while. I'll have to look at it more thoroughly to find out if it really does reach some reasonable limit of memory use, but for now, I'm keeping uwsgi in check with "reload-on-rss".
Reply all
Reply to author
Forward
0 new messages