Hi im not sure if this is related to garbage collection in python but im looking for some guidance in how it works under the hood. Below is a part of my program.
def get_data():
templist = []
'''
does stuff to fill templist with newest data
'''
return templist
def save_data(new_list, old_list):
'''
loops to check for updates.
if update, write to file
'''
if not old_list:
for n in new_list:
write_file(n)
else:
for n, o in zip(new_list,old_list):
if n[1] != o[1]:
write_file(n)
return new_list
comparelist = []
while True:
newlist = get_data()
comparelist = (save_data(newlist, comparelist))
I have checked with the id() function, and newlist gets passed as a new object each time to newlist, which then gets passed to comparelist (still memomery reference to templist.
My questions is this:
How and when objects are collected is generally a non issue, you should only care about how long you keep strong references to them around, which is mostly solved by scoping and knowing which objects you are keeping alive yourself.
When the object's reference count drops to zero, it's marked for deletion, when that deletion occurs is implementation dependent. In the reference CPython implementation, it runs immediately after it reaches 0 https://docs.python.org/3/c-api/refcounting.html#c.Py_DECREF
To save on a bit of time CPython will keep a few lists alive after they are deallocated, to be reused later to prevent creating completely new objects every time, so there's really nothing to worry about.