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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Also, the del statement seems to be a little bit faster than assigning None (similar to Java's style assigning null to a variable to free its memory ...).
To compare:
results in (running in idle3.4):
"Deletion of a name removes the binding of that name from the local or global namespace". No more, no less. It does nothing to the object the name pointed to, except decrementing its refcount, and if refcount is not zero, the object will not be collected even when GC runs.
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.