Python garbage collection can be that slow?

前端 未结 3 510
清酒与你
清酒与你 2021-01-01 18:45

I have a problem with my python application, and I think it\'s related to the python garbage collection, even if I\'m not sure...

The problem is that my application

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 19:11

    This is known garbage collector issue in Python 2.6 causing quadratic time for garbage collection when many objects are being allocated without deallocating any of them ie. population of large list.
    There are two simple solutions:

    1. either disable garbage collection before populating large lists and enable it afterwards

      l = []
      gc.disable()
      for x in xrange(10**6):
        l.append(x)
      gc.enable()
      
    2. or update to Python 2.7, where the issue has been solved

    I prefer the second solution, but it's not always an option;)

提交回复
热议问题