title: Django Python Garbage Collection woes
body: <p>After 2 days of debug, I nailed down my time-hog: the Python garbage collector.<br>
  My application holds a lot of objects in memory. And it works well.<br>
  The GC does the usual rounds (I have not played with the default thresholds of (700, 10, 10)).<br>
  Once in a while, in the middle of an important transaction, the 2nd generation sweep kicks in and reviews my ~1.5M generation 2 objects.<br>
  <strong>This takes 2 seconds!</strong>
  The nominal transaction takes less than 0.1 seconds.</p>
  
  <p>My question is what should I do?<br>
  I can turn off generation 2 sweeps (by setting a very high threshold - is this the right way?) and the GC is obedient.<br>
  When should I turn them on?<br>
  We implemented a web service using Django, and each user request takes about 0.1 seconds.<br>
  Optimally, I will run these GC gen 2 cycles <strong>between</strong> user API requests. But how do I do that?<br>
  My view ends with <code>return HttpResponse()</code>, <strong>AFTER</strong> which I would like to run a gen 2 GC sweep.<br>
  How do I do that? Does this approach even make sense?</p>
  
  <p>Can I mark the object that NEVER need to be garbage collected so the GC will not test them every 2nd gen cycle?<br>
  How can I configure the GC to run full sweeps when the Django server is relatively idle?</p>
  
  <p>Python 2.6.6 on multiple platforms (Windows / Linux).</p>