Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python del statement

Calling del on a variable in Python. Does this free the allocated memory immediately or still waiting for garbage collector to collect? Like in java, explicitly calling del has no effect on when the memory will be freed.

like image 670
totoromeow Avatar asked Feb 19 '13 23:02

totoromeow


People also ask

Should you use del in Python?

Never, unless you are very tight on memory and doing something very bulky. If you are writing usual program, garbage collector should take care of everything. If you are writing something bulky, you should know that del does not delete the object, it just dereferences it.

What is __ del __ in Python?

The __del__() method is a known as a destructor method. It is called when an object is garbage collected which happens after all references to the object have been deleted.

How do you use Del list in Python?

Python del operator:The del operator removes the item or an element at the specified index location from the list, but the removed item is not returned, as it is with the pop() method. So essentially, this operator takes the item's index to be removed as the argument and deletes the item at that index.


1 Answers

The del statement doesn't reclaim memory. It removes a reference, which decrements the reference count on the value. If the count is zero, the memory can be reclaimed. CPython will reclaim the memory immediately, there's no need to wait for the garbage collector to run.

In fact, the garbage collector is only needed for reclaiming cyclic structures.

As Waleed Khan says in his comment, Python memory management just works, you don't have to worry about it.

like image 146
Ned Batchelder Avatar answered Sep 16 '22 21:09

Ned Batchelder