Periodically delete long dicts / lists in Python good practice?

I wrote a long script that occasionally builds large dicts and / or lists, and I was wondering if performance could be improved by deleting them with del when I finish using them. Or is it normal practice to leave these objects hanging around to take care of garbage collection? What is the best practice here? Thanks.

+4
source share
3 answers

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[:] = [] # Both `bar` and `foo` still refer to the original list, but now it empty 

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”.

+7
source

del usually not required.

In CPython, objects disappear when there is no reference to them. del removes only one object reference; if there are other references to the object, it will stay around, and del really did nothing to improve your memory.

Conversely, simply reassigning the variable to another object (for example, creating a new empty list at the top of the loop) will also end with a smaller reference to the object, implicitly doing the same thing as del . If at this moment there are no other references to the object, it will be immediately released.

Also remember that local variables go away when the function returns, so you don’t have to explicitly tell del any names you define in the function.

An exception is circular links, where two (or more) objects refer to each other, but none of them can be reached through a name; Python periodically trash - collects them, but you can free them faster if you break the circle when you're done with objects. (This may require only del !). The circumstances in which this is useful are probably quite rare.

In IronPyothon (which runs on the .NET CLR) or Jython (which runs on the JVM), the optimal memory management strategies may be different because the underlying VM's garbage collector is used.

+1
source

One of the advantages of Python (compared to languages ​​like C) is that you don’t have to worry about the details of memory management at all, and you can focus on the goals of your program.

So my advice would be to not worry if you have no reason for this (e.g. python eats all the RAM).

0
source

Source: https://habr.com/ru/post/1468812/


All Articles