Deleting objects in memory using Python

Deleting objects in memory using Python

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.

I dont 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 dont manage memory by hand. You can help garbage collector, but its better to leave memory managment behind the scenes.

Deleting objects in memory using Python

Leave a Reply

Your email address will not be published. Required fields are marked *