Lucas Ou-Yang
Lucas Ou-Yang

Reputation: 5655

Django and limiting RAM usage: Recommended syntax, conventions, and strategies?

my django project is hosted using apache2 server has been consuming a ton of memory lately and i'm not sure if unusual or if something is being systematically done incorrectly in the project.

For the record, debug is set to False, memory leaks are being monitored and do not exist, the django queryset database has been cleared, and all static references are hosted on a seperate static application.

Here is the specific memory consumption:

2120 /home/path/apache2/bin/httpd.worker -f /home/path/apache2/conf/httpd.conf
46408 /home/path/apache2/bin/httpd.worker -f /home/path/apache2/conf/httpd.conf
47124  /home/path/apache2/bin/httpd.worker -f /home/path/apache2/conf/httpd.conf
4800 /home/path/apache2/bin/httpd.worker -f /home/path/apache2/conf/httpd.conf

Misunderstandings and questions:

What actually ends up costing memory and RAM? a queryset call? Defining a view? Everything? I know this is a really elementary question but I only have an abstract understanding of how servers and web applications interact.

For an industrial project, is:

Model.objects.all()

Really really bad? Or should everything be filtered as much as possible?

Is a rss of 46408 and 47124 considered overwhelmingly large for a django project? Or should I not even be concerned about optimizing the RAM usage further?

Every view in my project is being responded to with at least three SomeModel.objects.all() calls. Does this severely hurt performance, or does it not matter?

Thanks

Upvotes: 0

Views: 253

Answers (1)

jasisz
jasisz

Reputation: 1298

Not every Model.objects.all() results in evaluating all items. QuerySets in Django are lazy evaluated. If you are not doing stupid things like len(Model.objects.all()) which evaluates it right now, you probably do not end with those evaluations. At least not always - e.g. all paginators limits querysets and so on.

One of the most RAM consuming things in Django I experienced was when displaying select box in admin consisting of hundreds of thousands possible related objects... (ant that is why raw_id_fields are available).

Upvotes: 1

Related Questions