0

I have a chunk of python code that processes each element from a list, and then appends the processed element to another list:

unprocessed_list = [1,2,3,4,5,6,7,8,9,10]
processed_list = []
while len(l1) > 0:

    processed_list.append(process_element(l1[0]))
    del unprocessed_list[0]

    print "Unprocessed size:" ,sys.getsizeof(unprocessed_list)
    print "Processed size:" ,sys.getsizeof(processed_list)

I have re-written the code so that after an element is processed, it gets deleted with the del statement, and I see in the output that the unprocessed_list shrinks in size as the processed_list grows.

Does this mean that actual memory will be released, or do I need to do something additionally? Can/should I run the garbage collection manually?

This runs in python 2.7

Martin Taleski
  • 6,033
  • 10
  • 40
  • 78
  • I'm not sure if you can garbage collect manually, but python does it for you if there are no other references to the objects – joel goldstick Apr 04 '16 at 13:21
  • Yes you can garbage collect manually. See the gc module https://docs.python.org/2/library/gc.html – en_Knight Apr 04 '16 at 13:23
  • It might be released to be reused by the same process (python will decide when) but it won't be released for the operating system to reuse – cdarke Apr 04 '16 at 13:35

0 Answers0