Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a need to delete a large variable in python immediately after its use?

If I create a list that’s 1 GB, print it to screen, then delete it, would it also be deleted from memory? Would this deletion essentially be like deallocating memory such as free() in C.

If a variable is very large should I delete it immediately after use or should I let Python's garbage collector handle it ?

# E.g. creating and deleting a large list

largeList = ['data', 'etc', 'etc'] # Continues to 1 GB
print largeList
largeList = []   # Or would it need to be:  del largeList [:]
like image 455
Soap Avatar asked Oct 20 '25 05:10

Soap


1 Answers

Most of the time, you shouldn't worry about memory management in a garbage collected language. That means actions such as deleting variables (in python), or calling the garbage collector (GC) manually.

You should trust the GC to do its job properly - most of the time, micromanaging memory will lead to adverse results, as the GC has a lot more information and statistics about what memory is used and needed than you. Also, garbage collection is a expensive process (CPU-wise), so there's a good chance you'd be calling it too often/in a bad moment.

What happens in your example is that, as soon as ỳou largeList = [], the memory content previously referenced will be GC'd as soon as its convenient, or the memory is needed.

You can check this using a interpreter and a memory monitor:

#5 MiB used
>>> l1=[0]*1024*1024*32
#261 MiB used
>>> l2=[0]*1024*1024*32
#525 MiB used
>>> l1=[0]*1024*1024*32
# 525 MiB used

There are very rare cases where you do need to manage memory manually, and you turn off garbage collection. Of course, that can lead to memory leak bugs such as this one. It's worth mentioning that the modern python GC can handle circular references properly.

like image 64
loopbackbee Avatar answered Oct 21 '25 19:10

loopbackbee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!