Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Garbage collector for list

Tags:

python

I made simple loop which for each iteration, appends a number to a list. After the program has completed, will the memory used by the list be automatically freed?

if __name__ == '__main__':
    for i in range(100000):
         list.append(i)

Anyone can explain to me please?

like image 370
lloistborn Avatar asked Dec 05 '22 23:12

lloistborn


1 Answers

Yes, all memory is freed when the program is terminated. There is just no way in a modern operating system for you to reserve memory and not have it freed when the process is terminated.

Garbage collector is for freeing memory before the program terminates. This way long-running programs won't reserved all the resources of the computer.

If you have a big data structure, and you want the garbage collector to take care of it (free the memory used by it), you should remove all references to it after you're done using it. In this case simple del list would be sufficient.

like image 113
Scintillo Avatar answered Dec 10 '22 10:12

Scintillo