Python garbage collection can be that slow?

前端 未结 3 494
清酒与你
清酒与你 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;)

    0 讨论(0)
  • 2021-01-01 19:14

    If your problem really is the garbage collection, try explicitly freeing your objects when you're done with them using del().

    In general, this doesn't sound like a garbage collection problem, unless we're talking about terabytes of memory.

    I agree with S.Lott... profile your app, then bring code snippets and the results of that back and we can be much more helpful.

    0 讨论(0)
  • 2021-01-01 19:18

    Yes, it could be garbage collection, but it could also be some synchronisation with the C++ code, or something completely different (hard to say without code).

    Anyway, you should have a look at SIG for development of Python/C++ integration to find issues and how to speed up things.

    0 讨论(0)
提交回复
热议问题