del not equivalent to free (3). This does not force Python to free memory. Perhaps this will not free up memory. You should not completely associate it with memory usage.
The only thing del does is to remove the name from its scope. (Either remove the item from the collection or remove the attribute. But I assume that is not what you are talking about here.)
Effectively, it is:
del foo
This is equivalent to this:
del LOCAL_SCOPE['foo']
Thus, this does not free memory:
massive_list = list(range(1000000)) same_massive_list = massive_list del massive_list
... because all he does is delete the name massive_list . The base object still has a different name, same_massive_list , so it does not disappear. del not a secret trick for managing Python memory; this is just one of several ways to ask Python to call memory management.
(By the way, CPython is recounted + collected in a loop, and not collected using garbage. Objects are immediately freed as soon as the last link to them disappears. Garbage does not lie around waiting to be cleaned. Implementations do different things, PyPy, for example, collects garbage .)
Now, if the name you use is the only name for the list / dict / whatever, then del will necessarily cause its refcount to drop to zero, so it will be freed. But , since del semantics are really about deleting names, I would not use it in this case. I would just let the variable drop out of scope (if it was practical) or reassign the name to an empty list or None or something that makes sense for your program. You can even delete a list in place that will work, even if there are multiple names for the same list:
foo = list(range(1000000)) bar = foo foo[:] = []
You can do the same with dict with d.clear() .
The only place I would use del for the name is in the area of the class or module, where I temporarily need some kind of auxiliary value, but I really don't want to open it as part of the API. This is very rare, but this is the only case I have encountered where I really want the semantics of “delete this name”.