The int type is an immutable type. Therefore, the value once stored in memory cannot be changed.
If the values are the same, for example if there are multiple variables to which 1 is assigned, then all of those variables will refer to only one memory that stores 1. Will memory that is no longer referenced by variables be deleted? (Investigation required) (Will it be like a swift reference counter?)
qiita.rb
>>> int1 = 1
>>> int2 = 1
>>> print(id(int1), id(int2))#Output reference memory id
4383344848 4383344848
>>> int2 = 3
>>> print(id(int1), id(int2))
4383344848 4383344912
>>> int2 = 1
>>> print(id(int1), id(int2))
4383344848 4383344848
>>> intList = [1,2,3,4,5]
>>> print(id(intList[0])) #Index to store 1
4383344848
>>> print(id(intList[2])) #"3"Reference memory id
4383344912 #I just assigned it to int2"3"It's the same as the id of.(The number of reference variables for 3 has become zero, but I wonder if the memory will be cleaned immediately.)
Addendum 2020/10/04:
Will memory that is no longer referenced by variables be deleted? (Investigation required) (Will it be like a swift reference counter?) It's the same as the id of "3" that was assigned to int2 earlier. (The number of reference variables for 3 was zero, but I wonder if the memory will be cleaned immediately.)
You commented on the above answer in the comments section. Thank you @shiracamus.
Recommended Posts