4

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 [:]
1
  • largeList = [] would work fine. Commented Aug 14, 2014 at 8:54

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Whether memory is returned to the OS is platform and implementation dependent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.