Change in behaviour Python 3.7 > 3.8
> On 7 Feb 2020, at 05:27, Frank Millman <frank at chagford.com> wrote:
> I agree that __del__() is rarely useful, but I have not come up with an alternative to achieve what I want to do. My app is a long-running server, and creates many objects on-the-fly depending on user input. They should be short-lived, and be removed when they go out of scope, but I can concerned that I may leave a dangling reference somewhere that keeps one of them alive, resulting in a memory leak over time. Creating a _del__() method and displaying a message to say 'I am being deleted' has proved effective, and has in fact highlighted a few cases where there was a real problem.
> My solution to this particular issue is to explicitly delete the global instance at program end, so I do not rely on the interpreter to clean it up. It works.
I have faced the same problem with leaking of objects.
What I did was use the python gc to find all the objects.
First I call gc.collect() to clean up all the objects what can be deleted.
Then I call gc.get_objects() to get a list of all the objects the gc is tracking.
I process that list into a collections.Counter() with counts of each type of object
that I call a snapshot.
By creating the snapshots at interesting times in the services life I can diff the
snapshots and see how the object usage changes over time.
The software I work on has an admin HTTP interface used internally that I use to request
snapshots white the service is running.