Leaking Memory

connection.queries

Before Django 1.8 Since Django 1.8
Web Request signals.request_started.connect(reset_queries) signals.request_started.connect(reset_queries)
Python manage.py xxx unlimited queries_limit = 9000
  • connection.queries is only available if DEBUG is True.
  • connection.queries includes all SQL statements – INSERTs, UPDATEs, SELECTs, etc. Each time your app hits the database, the query will be recorded.
  • If DEBUG is True & before Django 1.8 & python manage.py xxx, You may find your Django processes are allocating more and more memory, with no sign of releasing it.

Queries

In [21]: from django.db import connection

In [22]: connection.queries
Out[22]: []

In [23]: from account.models import Profile

In [24]: Profile.objects.count()
Out[24]: 3

In [25]: connection.queries
Out[25]: [{u'sql': u'SELECT COUNT(*) FROM `account_profile`', u'time': u'0.000'}]

In [26]: from django.db import connections

In [27]: connections['default'].queries
Out[27]: [{u'sql': u'SELECT COUNT(*) FROM `account_profile`', u'time': u'0.000'}]

In [28]:

Delete/Clear

  • Web Request

    # Register an event to reset saved queries when a Django request is started.
    def reset_queries(**kwargs):
        for conn in connections.all():
            conn.queries_log.clear()
    signals.request_started.connect(reset_queries)
    
  • Python manage.py xxx

    from django.db import reset_queries
    reset_queries()
    

References

[1] Paul Tarjan@StackOverflow, Python: Memory leak debugging

[2] Ticket@DjangoProject, Ceiling limit to connection.queries

[3] django/django@Github, Fixed #3711, #6734, #12581 -- Bounded connection.queries.

[4] Releases@DjangoProject, Django 1.8 release notes

[5] Docs@DjangoProject, How can I see the raw SQL queries Django is running?

results matching ""

    No results matching ""