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
delis not really what you want. See stackoverflow.com/questions/6146963/…deloperator is sufficient to allow the value referenced byc.ins[someuniqueid]to be garbage collected? If so, yes (unless something else still holds a reference).delwil 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. Seesys.getrefcount()docs.python.org/3/library/sys.html#sys.getrefcountc.ins[someuniqueid]is deleted, sincedis also a reference to it. Only if you also dodel dwill 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.del.