4
l = [1, 2, 3]
del l

From the python docs:

It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

Does this mean that the list object, in this case l, will not be deleted even when the interpreter exits? I mean when the program exits, aren't all the objects reclaimed?

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • 1
    Check out the explanation https://stackoverflow.com/questions/21053380/what-does-del-do-exactly – Vaebhav Oct 17 '20 at 15:09
  • 1
    When the interpreter exits, all memory used by the process will be released. Just because the `__del__` method may not be called doesn't mean that the memory will persist when the process exits. – Tom Karzes Oct 17 '20 at 15:11
  • @Tom Karzes But it may be called when the interpreter exits, right? –  Oct 17 '20 at 15:35
  • @Brobdingnagian According to the quote in your post, it may or may not be called. It is not guaranteed to be called. So you can't rely on it. The memory will be freed, so that's not an issue. It's a question of whether there are other things that you want your `__del__` method to do. It may depend on how the interpreter is terminated. If it receives a hard kill signal, it has no choice and is forced to exit immediately, with no cleanup. – Tom Karzes Oct 17 '20 at 15:38
  • 1
    You are mixing several things in this answer. ``del`` deletes a *name*. ``__del__`` deletes *objects state*. The garbage collector frees *memory*. The three are only indirectly related, and each can happen without the other. – MisterMiyagi Oct 17 '20 at 15:51
  • @MisterMiyagi What is meant by "```__del__``` deletes objects state."? –  Oct 17 '20 at 15:54
  • The purpose of ``__del__`` is to clear the state represented by its object, e.g. to close open files, disconnect connected sockets, and such. – MisterMiyagi Oct 17 '20 at 16:01
  • @MisterMiyagi Is the memory reclaimed by the OS after the interpreter exits? –  Oct 17 '20 at 16:45

1 Answers1

3

It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

This statement implies that you should not rely on a custom __del__() method being executed upon interpreter exit.

The statement does not imply that the interpreter will create a memory leak.

In all cases when Python exits, all memory will be released.

See also

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • The memory is *always* released when Python exits. The quoted passage talks about how memory might leak while a process is running, but once a process exits, all its memory is reclaimed by the OS. – sepp2k Oct 17 '20 at 15:57
  • That quote is rather misleading, even for CPython. ``gc.collect()`` is not about unreferenced memory, but about memory referenced in a cycle. The CPython garbage collector handles cycles with ``__del__`` just fine these days (since ~Py3.4). Objects are always (memory) deallocated on exit, they might just not be finalised (i.e. ``__del__``'d). The garbage collector is precisely what does detect and free cyclic references. And some more... – MisterMiyagi Oct 17 '20 at 15:57
  • @sepp2k. Calling `__del__` has nothing to do with memory leaking during the execution either – Mad Physicist Oct 17 '20 at 16:02
  • 1
    @MadPhysicist When I said "the quoted passage" I was talking about a quote that was included in the original version of this answer (and that quote was specifically about memory leaks), not about the quote in the question. – sepp2k Oct 17 '20 at 16:05