I guess I am missing something here, but when I am doing forcefully garbage collection using gc.collect(), I shouldn't be able to access id(10) since that is not being referenced by any of the variable and even if i assume that id(10) call is creating the variable, is it creating at the same location? that is wierd
import gc
import time
x = 10
y = x
print('10 id is: ', id(10))
print('x id is: ',id(x))
print('y id is: ',id(y))
x = x+1
y = 11
print('x id is: ',id(x))
print('y id is: ',id(y))
# above is justified
gc.collect(generation=2)
time.sleep(2)
print(id(10))
# but how come is above statement giving the value of id(10) as earlier?
When I run this snippet:
line1: 10 id is: 140709303001776
line2: x id is: 140709303001776
line3: y id is: 140709303001776
line4: x id is: 140709303001808
line5: y id is: 140709303001808
line6: 140709303001776
so how come in line6, i am able to get the id(10) as the same I had allocated earlier, since now x and y are pointing to different memories and I have garbage collected before printing line6
Please help me in this snippet as to how is the memory allocation working as this got me confused.