Can anyone provide an explanation for why this is happening?
>>> for i2 in range(10):
... print i2
... gc.collect()
...
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
The result of the gc.collect() is the number of unreachable objects:
gc.collect([generation])
With no arguments, run a full collection. The optional argument generation may be an integer specifying which generation to collect (from0to2). AValueErroris raised if the generation number is invalid. The number of unreachable objects found is returned.
When you enter that program into Python interactively, the result of all those calls is being output by the REPL (read-eval-print loop). This is no different to when you enter 42+7 into Python interactively and it echoes back 49 despite there being no indication you wanted it printed (with, for example, a print call).
This output you're seeing is therefore all the zero return values from gc.collect(), interspersed amongst your "real" output from the range printing.
If you ran this as a real script, you'd only see the numbers 0..9 since the REPL is not involved. Or you could equally prevent the return value from being output by doing something with it, such as:
>>> for i2 in range(4):
... print i2
... junk = gc.collect()
...
0
1
2
3
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