[Python-Dev] Py_Finalize does not release all memory, not even closely

"Martin v. Löwis" martin at v.loewis.de
Tue Apr 11 13:50:30 CEST 2006

Several places in the documentation claim that Py_Finalize will
release all memory:


says that literally, and


suggests it is a bug when memory is not freed in Py_Finalize.

This has left people to believe that this is a bug:


However, I don't see any chance to make this promise even remotely.
Objects allocated in extension modules, and held in global variables
(e.g. socketmodule.c:socket_error, socket_herror, socket_gaierror,
socket_timeout) will never be released, right?

And because of the small objects allocator, their pool will remain
allocated, right? And, then, the arena.

So ISTM that invoking Py_Finalize after importing socket will yield
atleast 256KiB garbage. Of course, that's not real garbage, because
the next Py_Initialize'd interpreter will continue to allocate from
the arenas. But still, the actual objects that the modules hold
on to will not be reclaimed until the process terminates.

Please correct me if I'm wrong.


More information about the Python-Dev mailing list