Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why disable the garbage collector?

Tags:

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?

like image 889
gerrit Avatar asked Dec 10 '13 13:12

gerrit


People also ask

Why is garbage collection a problem?

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.

Is garbage collector necessary?

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.

Can you disable garbage collection?

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.

What is the purpose of garbage collectors?

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.


1 Answers

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().

like image 141
unutbu Avatar answered Dec 22 '22 07:12

unutbu