Python Forum
Why doesn't gc delete an object without forcing a garbage collection call? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why doesn't gc delete an object without forcing a garbage collection call? (/thread-16762.html)



Why doesn't gc delete an object without forcing a garbage collection call? - AlekseyPython - Mar-13-2019

Python 3.7.2

I created an event loop, run the task in it, but one of the objects wasn't deleted. After closing the event loop, I forced the garbage collection and in the debugger I finally got into the __del__ method of my object:
def __call__(self):
    loop = self._get_event_loop()
    try:
        loop.run_until_complete(self._create_workers())
    except Exception as err:
        print('Operation sucessfully failed!)
        raise err
    finally:
        #close the created event loop
        loop.close()
        
    gc.collect() #in this place object, created in event loop finally delete
If I place the call gc.collect() before closing the event loop, then the object will remain alive (I don’t get to the __del__ method and my data is not written to the database).

Why?


RE: Why doesn't gc delete an object without forcing a garbage collection call? - Larz60+ - Mar-13-2019

see: https://docs.python.org/3/library/gc.html?highlight=gc%20collect#gc.collect
Quote:The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the particular implementation, in particular float.



RE: Why doesn't gc delete an object without forcing a garbage collection call? - AlekseyPython - Mar-14-2019

(Mar-13-2019, 05:43 PM)Larz60+ Wrote: see: https://docs.python.org/3/library/gc.html?highlight=gc%20collect#gc.collect
Quote:The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the particular implementation, in particular float.

You brought a good theoretical reference, but other questions worry me in the topic:
1. Why does garbage collection not start independently, but only after calling the collect() method?
2. Why is the collect() method effective only after closing the event loop?


RE: Why doesn't gc delete an object without forcing a garbage collection call? - Larz60+ - Mar-14-2019

Quote:Why does garbage collection not start independently, but only after calling the collect() method?
Python schedules garbage collection automatically. You don't have to call collect.
I've written many thousands of lines of python code, and haven't been concerned in the least about garbage collection.
It's always performed as stated and has never caused a problem for me.


RE: Why doesn't gc delete an object without forcing a garbage collection call? - AlekseyPython - Mar-15-2019

(Mar-14-2019, 09:41 AM)Larz60+ Wrote:
Quote:Why does garbage collection not start independently, but only after calling the collect() method?
Python schedules garbage collection automatically. You don't have to call collect.
I've written many thousands of lines of python code, and haven't been concerned in the least about garbage collection.
It's always performed as stated and has never caused a problem for me.

if I don't call gc.collect() method __del__, which writing accumulated data in my database, not performed. But if I call it, then method __del__ calling and data writing successfully. This is evident not only by the debugger, but also by the presence of records in the database.


RE: Why doesn't gc delete an object without forcing a garbage collection call? - micseydel - Mar-19-2019

__del__ is not guaranteed to run, even when Python exits. If you need to do cleanup external to your program, you need to manage that manually.