[Python-Dev] C API for gc.enable() and gc.disable()

Kevin Jacobs <jacobs@bioinformed.com> bioinformed at gmail.com
Fri Jun 20 16:15:49 CEST 2008


+1 on a C API for enabling and disabling GC.

I have several instances where I create a large number of objects non-cyclic
objects where I see huge GC overhead (30+ seconds with gc enabled, 0.15
seconds when disabled).

+1000 to fixing the garbage collector to be smart enough to self-regulate
itself better.

In the mean time, I use the following context manager to deal with the
hotspots as I find them:

class gcdisabled(object):
  '''
  Conext manager to temporarily disable Python's cyclic garbage collector.
  The primary use is to avoid thrashing while allocating large numbers of
  non-cyclic objects due to an overly aggressive garbage collector behavior.

  Will disable GC if it is enabled upon entry and renable upon exit:

  >>> gc.isenabled()
  True
  >>> with gcdisabled():
  ...   print gc.isenabled()
  False
  >>> print gc.isenabled()
  True

  Will not reenable if GC was disabled upon entry:

  >>> gc.disable()
  >>> gc.isenabled()
  False
  >>> with gcdisabled():
  ...   gc.isenabled()
  False
  >>> gc.isenabled()
  False
  '''
  def __init__(self):
    self.isenabled = gc.isenabled()

  def __enter__(self):
    gc.disable()

  def __exit__(self, type, value, traceback):
    if self.isenabled:
      gc.enable()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20080620/3c7b7a48/attachment.htm>


More information about the Python-Dev mailing list