Does garbage collection make python slower?

OK, so we are developing a network application in which the user can download their own python scripts to make a decision on the algorithm. Our code contains c and cython and python modules.

Since avoiding latency, occupied memory and minimal processing is critical for us, we were wondering if it was reasonable and efficient (in terms of performance) to turn off garbage collection and free up memory ourselves.

+7
source share
6 answers

gc.disable disables the circular garbage collector. Objects will still be collected when the refcount drops to zero anyway. Therefore, if you have many circular links, it does not matter.

Are you talking about creating a custom Python assembly and disabling the GC to count the number of links?

+11
source

Just let the language do what it wants to do, and if you find that you have a real problem, go back and post about it. Otherwise, it is premature optimization.

+18
source

Just design the application so that it is functionally correct. Once you get the right application, run the profiler and determine where the slow bits are. If you are worried about user script performance, you are probably focusing on the wrong one.

The shorter answer is that it is probably unreasonable and inefficient to try to optimize before the initial development is complete.

+5
source

Garbage collection is slower. It also makes everything much less error prone. Especially if it makes sense to run user-loaded scripts, it's hard for me to believe that a compromise will work well; if you have any leaks or doubles, you now have a DoS vulnerability if someone can figure out how to run it.

+4
source

I spent a lot of time working in languages ​​with automatic garbage collection, and I can say almost unilaterally that trust in garbage collection will be faster and more reliable than my own solution.

+2
source

CPython (the original and most used Python) uses the ref counting approach to collect garbage: objects that are no longer referenced are immediately freed. Therefore, if you do not create any loops, the garbage collector, which exists only to detect loops, should not receive many calls.

+1
source

All Articles