#34865: DatabaseWrapper are not GC and connections are not closed
-------------------------------------+-------------------------------------
     Reporter:  Fábio Domingues      |                    Owner:  Priyank
                                     |  Panchal
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Patryk Zawadzki):

 Florian, it does clean up ''eventually'' thanks to the garbage collection,
 but garbage collection is a last resort method of freeing unused memory.

 There are two important problems with relyin on GC:

 1. It happens very rarely (the default thresholds are `(700, 10 10)`,
 which means the deepest collection cycle—generation 2—only runs every
 70,000 dereferenced objects. That's very rare; in a simple Django
 application, that can mean a hundred requests.
 2. It also locks the global interpreter lock and effectively freezes the
 entire Python process (and all its threads) until it goes through all
 uncollectable objects. The more objects it finds (not just the ones with
 reference cycles but also everything else they hold a reference to), the
 longer the freeze. In extreme cases, it can randomly add seconds to your
 response times.

 Aggregation of garbage leads to memory fragmentation as unused memory
 cannot be reused until the garbage collection thresholds are met, which
 leads to the overall memory use slowly creeping up.

 What makes it worse is that in this case, it holds a reference to a
 `psycopg` connection object, holding all of the psycopg internals hostage
 and preventing proper finalization of the connection (and, I assume, in
 Django 5.x also preventing the connection from being reclaimed by the
 connection pool? I did not verify that).
-- 
Ticket URL: <https://code.djangoproject.com/ticket/34865#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070194d5c1e87c-0c570d5f-9130-4b02-aaf5-c22b707388c9-000000%40eu-central-1.amazonses.com.

Reply via email to