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

Question:

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 [:]
Asked By: Soap

||

Answers:

Using “die” —> Deletion of a name removes the binding of that name from the local or global namespace. It releases memory for sure but not all the memory is released.

NOTE: When a process frees some memory from HEAP, it releases back to the OS only after the process dies.

So, better leave it for the Garbage Collector.

Answered By: Abhishake Gupta

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.

Answered By: loopbackbee
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.