0
l = [1000, 2000, 3000]
print(l)
del l
for i in range(5):
    print('Hello')

So is the memory of the 'list' object gets immediately freed after printing before even the for loop starts executing?

  • 3
    Why would it be freed? The variable `l` still refers to the list. – jasonharper Jun 04 '20 at 17:59
  • @jasonharper Thanks!! Such a silly question. –  Jun 04 '20 at 18:01
  • To keep things complicated, python caches small integers. `l = [1000, 2000, 3000]` followed by `del l` would more closely fit your question. Or perhaps `print([1000, 2000, 3000])` – tdelaney Jun 04 '20 at 18:02
  • In cpython, (and assuming you picked an integer > 256), the refcounts of the list and its members would go to zero and they would be freed before the print statement. "freed" means "returned to python's heap where their memory can be reused for other python objects", they won't be fully freed from the process. – tdelaney Jun 04 '20 at 18:05
  • @tdelaney *"they would be freed before the print statement"* - Wouldn't the print require it? –  Jun 04 '20 at 18:10
  • No, why would it? – juanpa.arrivillaga Jun 04 '20 at 18:55
  • @juanpa.arrivillaga To print the list on ```sys.stdout```. –  Jun 04 '20 at 18:57
  • You don't del until after you print... – juanpa.arrivillaga Jun 04 '20 at 19:02
  • @juanpa.arrivillaga Okay, so python deletes the name associated with the object after printing in my case, right? But I didn't understand: *"freed" means "returned to python's heap where their memory can be reused for other python objects", they won't be fully freed from the process.* - How it won't be fully freed if the memory is reclaimed? –  Jun 04 '20 at 19:20
  • @NSR sorry for the late reply. Your print doesn't use the list. As for what "free" means, cpython uses heaps for memory - memory is allocated from the system in larger blocks and carved up for python objects as needed. I mentioned it because freeing a python object doesn't reduce overall process memory usage. But its free to make more python objects. – tdelaney Jun 04 '20 at 19:36
  • "Okay, so python deletes the name associated with the object after printing in my case, right?" No, **you told python to do that** here: `del l`. I'm not sure what you are asking about exactly. – juanpa.arrivillaga Jun 04 '20 at 20:00
  • @juanpa.arrivillaga But ```print(l)``` is before that. –  Jun 04 '20 at 20:05
  • @tdelaney How my print doesn't use a list? - I believe that ```print(l)``` does that. –  Jun 04 '20 at 20:13
  • 1
    @NSR ... exactly, it happens *before* that so it *isn't relevant*? Its not available to be freed until the reference count reaches 0, which is when `del l` happens, which is **after** you `print(l)` so it doesnt matter. – juanpa.arrivillaga Jun 04 '20 at 20:25
  • @juanpa.arrivillaga Let's suppose ```del l``` isn't present. Then will the list object be freed after the program exits? –  Jun 04 '20 at 20:34
  • @NSR - Sorry, I thought you were referring to the second print. My mistake. – tdelaney Jun 04 '20 at 20:46
  • @tdelaney No problem --- *"I mentioned it because freeing a python object doesn't reduce overall process memory usage."* What is this process memory usage? I searched on the web but didn't find anything relevant. Please explain. –  Jun 04 '20 at 20:53
  • 1
    Operating systems have commands to tell you how much memory a process uses. On linux you could do `ps o size,vsize,command ax | grep python` to get size and virtual memory size of python processes. If you use a large amount of memory temporarily then free it, it can be puzzling why your process memory size is still large. – tdelaney Jun 04 '20 at 21:00
  • 1
    @NSR "Then will the list object be freed after the program exits?" Yes, the *entire process memory* is reclaimed by the OS at that point, that wouldn't really have anything to do with python per se, though, that is an OS level thing, and it has nothing to do with reference counting. – juanpa.arrivillaga Jun 04 '20 at 21:01

0 Answers0