Does python garbage-collect at the end of an iteration in a loop? -
please observe simple code:
import random while true: l = list( str(random.random()))
question: if let run, python run out of memory?
reason asking:
first iteration of loop, list created, , 'l' assigned represent list. next iteration of loop, list created, 'l' yanked previous list , and assigned new list. previous list has lost reference. previous list going garbage collected? if not @ end of each iteration, hope?
having said that, expand scenario bit further multiprocessing:
import random while true: l1 = list( str(random.random())) pseudo: multiprocessing.queue.put(l1) # how l1 handled here? # l1 .copy()-ed queue or referenced queue? # l1 destoryed in process (this while loop) @ end of iteration?
the primary means of garbage collection reference counting in cpython (the reference implementation of language). when there no longer references object, memory occupies freed , can reused other python objects. (it may or may not ever released operating system.) there few exceptions of objects never freed: smallish integers, interned strings (including literals), empty tuple, none
.
so answer initial question, l
going reassigned new list on each iteration. @ point, previous list has no references , memory released immediately.
with regard second example, putting multiprocessing
queue is, of necessity, copy operation. object must serialized ("pickled" in python parlance) sent new process, has own memory space , can't see original process's memory. when, in loop, reassign li
next list, previous list has no references and, again, released.
at end of loop, l
or l1
variable still refers list: 1 created in last iteration of loop. if want release object, del l
or del l1
respectively.
ps -- when objects contain references (either directly, or indirectly through chain of other objects), referred cyclic reference. these aren't collected automatically reference counting , python has separate garbage collector runs periodically clean them up.
Comments
Post a Comment