lloistborn
lloistborn

Reputation: 373

Python - Garbage collector for list

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?

Upvotes: 4

Views: 2766

Answers (5)

buydadip
buydadip

Reputation: 9437

You needn't worry about freeing memory in python, since this is an automatic feature of the program. Python uses reference counting to manage memory, when something is no longer being referenced, python will automatically allocate memory accordingly. In other words, removing all references to the list should be enough to free the memory that was allocated to it.

That said, if your program is huge, you may use gc.collect() to make the garbage collector free some memory. However, this is generally unnecessary, as Python's garbage collector is designed to do its job well enough.

Moreover, although this is not recommended, and generally never very useful, you may also disable Python's automatic garbage collector using gc.disable(), which allows you as the user to allocate the memory manually, in an almost C style approach.

Upvotes: 2

Paulo Scardine
Paulo Scardine

Reputation: 77359

Yes, the memory will be freed when the program terminates and on most implementations it will also be freed when the reference count to that list reaches zero, i.e., when there is no variables in scope pointing to that value.

You can also manually control the GC using the gc module.

If you are just iterating over the list and the list is big enough to get you worried about memory consumption, you probably should check Python generators and generator expressions.

So instead of:

for i in range(100000):
    results.append(do_something_with(i))

for result in results:
    do_something_else_with(result)

You can write:

partial =  (do_something_with(i) for i in range(100000))
for result in partial:
    do_something_else_with(result)

Or:

def do_something(iterable):
    for item in iterable:
        yield some_calculation(item)

def do_something_else(iterable):
    for item in iterable:
        yield some_other_calculation(item)

partial = do_something(range(100000))
for result in do_something_else(partial):
    print result

And so on... This way you don't have to allocate the whole list in memory.

Upvotes: 3

Scintillo
Scintillo

Reputation: 1704

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.

Upvotes: 8

Loupi
Loupi

Reputation: 580

All the memory will be freed after termination but if you want to be efficient in creating a list this large during execution use xrange instead which generates the number on the fly.

    alist = [i for i in xrange(10000)]

Upvotes: 1

Ignotas
Ignotas

Reputation: 41

Yes, the operating system takes care and frees the memory used by a process after it terminates. Python has nothing to do with it.

However, Python itself has automatic garbage collection, so it frees all of the memory that is no longer necessary while your Python program is running.

Finally, you probably should just use:

if __name__ == '__main__':
    list = range(100000)

to achieve exactly the same thing you have written.

Upvotes: 3

Related Questions