When are objects garbage collected in python?
There is a lot of detail in the source code for CPython: http://svn.python.org/view/python/trunk/Modules/gcmodule.c?revision=81029&view=markup
Any time a reference count drops to zero, the object is immediately removed.
293 /* Python's cyclic gc should never see an incoming refcount
294 * of 0: if something decref'ed to 0, it should have been
295 * deallocated immediately at that time.
A full collection is triggered when the number of new objects is greater than 25% of the number of existing objects.
87 In addition to the various configurable thresholds, we only trigger a
88 full collection if the ratio
89 long_lived_pending / long_lived_total
90 is above a given value (hardwired to 25%).
When is the memory released?
I was only able to fish out this information.
781 /* Clear all free lists
782 * All free lists are cleared during the collection of the highest
generation.
783 * Allocated items in the free list may keep a pymalloc arena
occupied.
784 * Clearing the free lists may give back memory to the OS earlier.
785 */
According to this, Python may be keeping your object in a free list for recycling even if you drop its refcount to zero. I am unable to explicitly find when the free call is made to give memory back to the operating system, but I imagine that this is done whenever a collection is made and the object is not being kept in a free list.
Does the collection impact performance?
Any non-trivial garbage collector I have heard of requires both CPU and memory to operate. Therefore, yes, there is always an impact on performance. You'll have to experiment and get to know your garbage collector.
Programs that require real time responsiveness I have run into issues with, since garbage collectors don't grant me control over when they run or for how long they do. Some peculiar cases can cause excessive memory use as well, an example being Python's knack for keeping free lists.