7

I am trying this approach to delete an object in python. I read the documentation of Python stating that garbage collector will automatically delete the object that is not referenced.

def check():
    class newOb():
        def __init__(self,value):
            self.value = value
            print self.value
            return None

    class ob:
        ins = {}
        def cr(self,someuniqueid,value) :
            newV = newOb(value)
            ob.ins[someuniqueid] = newV ## saving this object refernce to the ob class ins dictionary
            return newV

    #### Accessing Object ###
    someuniqueid  = 12
    c = ob()
    d = c.cr(someuniqueid,123)
    print d.value ## will print 123
    # now deleting the associated object
    del c.ins[someuniqueid]

check()

At the last step, I am removing the object reference from the memory is using above procedure will delete the object from memory

If not then what is wrong with code and how to correct it

cs95
  • 379,657
  • 97
  • 704
  • 746
user3411846
  • 178
  • 1
  • 3
  • 13
  • It's not really clear what you're trying to do. If you want to remove an entry from a dictionary, `del` is not really what you want. See https://stackoverflow.com/questions/6146963/when-is-del-useful-in-python – pvg Jun 06 '17 at 07:49
  • 1
    What is your question? Are you asking if the `del` operator is sufficient to allow the value referenced by `c.ins[someuniqueid]` to be garbage collected? If so, yes (unless something else still holds a reference). – dimo414 Jun 06 '17 at 07:50
  • `del` wil decrement the reference count. When the reference count is zero the object is a candidate for removal - the exact timing of the garbage collector is complex and is rarely immediate. See `sys.getrefcount()` https://docs.python.org/3/library/sys.html#sys.getrefcount – cdarke Jun 06 '17 at 07:52
  • Your object won't be garbage collected when the reference `c.ins[someuniqueid]` is deleted, since `d` is also a reference to it. Only if you also do `del d` will the object (which you can't access any more) be deleted. And garbage collection is not guaranteed. It *may* happen any time after the last reference goes away, but exactly when it happens is an implementation detail that you shouldn't rely upon. – Blckknght Jun 06 '17 at 07:52
  • @Blckknght then what changes should i made because whole application is using this kind of logic only – user3411846 Jun 06 '17 at 07:56
  • I have no idea what you are asking. What kind of logic? Why do you care when exactly an object is deleted in memory? In Python, you shouldn't ever need to care (except *maybe* if you're debugging a program because it's too much memory). – Blckknght Jun 06 '17 at 08:35
  • @Blckknght I have data intensive task which uses too much data and requires too much in memory calculations that's why i need to be assured that the python does not goes out of memory – user3411846 Jun 06 '17 at 08:37
  • 2
    Then you probably need to ask a question more closely related to your real code. This question doesn't make a whole lot of sense. It's likely that you can reduce the memory footprint of your program more effectively by using better data structures or avoiding loading all the data in memory at one time, rather than messing around with `del`. – Blckknght Jun 06 '17 at 08:42
  • @Blckknght i have shown you the real code only – user3411846 Jun 06 '17 at 11:21
  • What's wrong with calling `object.__del__()`? – Danny Holstein Mar 29 '23 at 22:41

2 Answers2

13

You would need to do del d as well, since d is also holding a reference to the same object. Calling del will only decrement the reference count and remove the particular reference from usage, but the actual in memory object is not garbage collected until the reference count hits 0.

cs95
  • 379,657
  • 97
  • 704
  • 746
7

I don't know what do you mean by writing:

If not then what is wrong with code and how to correct it

When you use del statement you delete a reference to an object. It will use up memory untill garbage collector is invoked. Remember that this can be a time-consuming process and not necessary if the process has enough memory to continue executing.

Generally speaking Python does not perform C++-like destructor bahaviour.

A quote from "Expert Python Programming":

The approach of such a memory manager is roughly based on a simple statement: If a given object is not referenced anymore, it is removed. In other words, all local references in a function are removed after the interpreter:

• Leaves the function

• Makes sure the object is not being used anymore.

Under normal conditions, the collector will do a nice job. But a del call can be used to help the garbage collector by manually removing the references to an object manually.

So you don't manage memory by hand. You can help garbage collector, but it's better to leave memory managment behind the scenes.

Community
  • 1
  • 1
gonczor
  • 3,994
  • 1
  • 21
  • 46