Python Forum
Why doesn't gc delete an object without forcing a garbage collection call?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't gc delete an object without forcing a garbage collection call?
#1
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?
Reply
#2
see: https://docs.python.org/3/library/gc.htm...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.
Reply
#3
(Mar-13-2019, 05:43 PM)Larz60+ Wrote: see: https://docs.python.org/3/library/gc.htm...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?
Reply
#4
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.
Reply
#5
(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.
Reply
#6
__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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 222 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,265 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  'int' object is not subscriptable after API call ed8484 1 1,765 Sep-18-2021, 02:06 PM
Last Post: ed8484
  how to call an object in another function in Maya bstout 0 2,042 Apr-05-2021, 07:12 PM
Last Post: bstout
  threadlocals are garbage collected before thread ends akv1597 0 1,774 Mar-09-2021, 12:13 PM
Last Post: akv1597
  Images are storing in RAM and don't get garbage collected MaxRicik 4 2,668 Jan-07-2021, 02:59 AM
Last Post: deanhystad
  I need advise with developing a brute forcing script fatjuicypython 11 4,925 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  delete a Python object that matches an atribute portafreak 2 2,134 Feb-19-2020, 12:48 PM
Last Post: portafreak
  How to mock an object that is created during function call? Schlangenversteher 0 1,946 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Forcing input from pre-defined list. scotty501 11 5,989 Jun-18-2019, 01:49 PM
Last Post: scotty501

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020