1

In an SO thread a responder makes this suggestion:

Careful coding can eliminate almost all dynamic memory allocation, in which case you can completely disable GC!

I have read up on the subject of dynamic memory allocation, but I can't figure out how it can be avoided with "careful coding" in Python. Is this an empty claim, or is there a method or discipline that may be employed?

Perhaps the suggestion is really referring to avoiding reference count cycles. In this case too, I'd be interested to learn if techniques exist for avoiding them.

Theo d'Or
  • 783
  • 1
  • 4
  • 17
  • 1
    This strikes me as a suspicious assertion. For one, memory allocation and GC is abstracted away from the programmer, so I don't see how any amount of care can provide any guarantees of safety. From what I can tell, all object memory will be on the heap, and risking even a small leak doesn't seem worth any performance gains. Sure, GC is slow, but I'd trust that Python has optimized it not to run more often than necessary. Following all of this person's suggestions sounds to me like they might as well just code in C instead of trying to make Python something it isn't. – ggorlen Jan 07 '19 at 22:58
  • I guess this is more a wish than a fact. While you can argue about the meaning of "almost" there are things happening in Python that will always consume memory. Every function call for example creates a namespace that will remain if not garbage collected and most operations do at least one function call in the background. – Klaus D. Jan 07 '19 at 23:06
  • Considering that `300+301` involves dynamic allocation for the 601 and destroying the operands (if they are also computation results rather than literals), I don’t see how this is possible. I suspect it’s a terminological error; your idea of circular references is plausible, since those are what necessitate GC. – Davis Herring Jan 08 '19 at 06:23

0 Answers0