I have the following hypothetical Python program that uses some of the garbage collector functions (documentation):
import gc
# Should turn automatic garbage collection off
gc.disable()
# Create the string
a = "Awesome string number 1"
# Make the previously assigned string unreachable
# (as an immutable object, will be replaced, not edited)
a = "Let's replace it by another string"
# According to the docs, collect the garbage and returns the
# number of objects collected
print gc.collect()
The program prints 0
, which seems weird to me because:
str
object is created and is referenced by a
.str
object is created and a
and now referenced by a
.str
object was never deleted because we have turned off automatic garbage collection, thus it still exists in the memory.I would greatly appreciate an explanation of why it's not collected.
P.S. I do know that Python treats some objects (including integers from -3 to 100, as far as I remember) as singletons, but there is no way these particular strings are such objects.
P.P.S I am running it as a whole program, not in the shell
The gc module in Python is only responsible for collecting circular structures. Simple objects like strings are reclaimed immediately when their reference count becomes 0. gc doesn't report on simple objects, and disabling it doesn't prevent strings from being reclaimed.
Extra advanced detail: even if the gc module were responsible for all object reclamation, the first string still wouldn't be collected at the gc.collect()
call, because there's still a live reference to that string: the reference in the co_consts
tuple of the script's code object.
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