Pythons gc.disable
disables automatic garbage collection. As I understand it, that would have quite some side-effects. Why would anyone want to disable automatic garbage collection, and how could one effectively manage memory without it?
If a significant amount of process time is spent in a garbage collection, the number of collections is too frequent or the collection is lasting too long. An increased allocation rate of objects on the managed heap causes garbage collection to occur more frequently.
It is not strictly necessary. Given enough time and effort you can always translate a program that depends on garbage collection to one that doesn't.
A quick workaround is to disable the System. gc () and force a Full GC. The –XX:+DisableExplicitGC command can be passed as a java parameter that disables System. gc() for the JVM.
The garbage collector considers unreachable objects garbage and releases the memory allocated for them. During a collection, the garbage collector examines the managed heap, looking for the blocks of address space occupied by unreachable objects.
One use for disabling the garbage collector is to get more consistent results when timing the performance of code. The timeit
module does this.
def timeit(self, number=default_number): if itertools: it = itertools.repeat(None, number) else: it = [None] * number gcold = gc.isenabled() gc.disable() ...
In Python2 and up to Python3.2 gc.disable()
is also used to avoid a bug caused by garbage collection occurring between fork
and exec
. The problem seems to have been fixed in Python3.3 without needing to call gc.disable()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With