When does python delete variables?

I know that python has an automatic garbage collector, and therefore it should automatically delete variables when there are no more references to them.

My impression is that this does not happen for local variables (inside a function).

def funz(z): x = f(z) # x is a np.array and contains a lot of data x0 = x[0] y = f(z + 1) # y is a np.array and contains a lot of data y0 = y[0] # is x still available here ? return y[0], x[0] 

is del x the correct way to save memory?

  def funz(z): x = f(z) # x is a np.array and contains a lot of data x0 = x[0] del x y = f(z + 1) # y is a np.array and contains a lot of data y0 = y[0] return y[0], x[0] 

EDIT: I edited my example so that it looks more like my real problem. In my real problem, x and y are not a list, but a class that contains different long np.array

EDIT: I can run the code:

 x = f(z) x[0] print(x0) y = f(z + 1) y0 = [0] print( y0) 
+8
python garbage-collection memory
source share
4 answers

Implementations use reference counting to determine when to remove a variable.

After the variable goes out of scope (as in your example), if there are no other references to it, then the memory will be freed.

 def a(): x = 5 # x is within scope while the function is being executed print x a() # x is now out of scope, has no references and can now be deleted 

Besides the dictionary keys and items in lists, there are usually very few reasons to manually delete variables in Python.

Although, as stated in the answers to this question , using del can be useful for showing intent.

+6
source share

It is important to separate two concepts: names and meanings. A variable in Python is a name that refers to a value. Names have scope: when you define a local variable (by assigning a value to a name), the scope variable is the current function. When the function returns, the variable leaves. But this does not mean that the value is leaving.

Values โ€‹โ€‹have no scope: they adhere until there are no more names related to them. You can create a value in a function and return it from this function by forcing the name outside the function to refer to the value, and the value will not be fixed until some future, when all references to it have disappeared.

Read more (including photos!) Here: Facts and myths about Python names and signs .

+3
source share

Write the things you want to clear from memory in separate functions. In your example, you can do

  def xdef(z): x = f(z) # x is a np.array and contains a lot of data x0 = x[0] def funz(z): xdef(z) y = f(z + 1) # y is a np.array and contains a lot of data y0 = y[0] return y[0], x[0] 

This will result in an exception.

0
source share

It depends on the implementation and type of variable. For simple objects like int, there are some optimizations. For example, in CPython, a simple int will reuse the same memory, even after del been used. You cannot count on it, but it illustrates that things are harder than they appear.

Remember that when you del you delete the name, not necessarily an object.
For example:

 # x is a np.array and contains a lot of data 

It will be more precisely formulated as:

 # x references a np.array which contains a lot of data 

del will decrease the reference count on this object, but even when it drops to zero, it will not be guaranteed that garbage collection will be collected in the near future.

Have a look at the gc module for an explanation and inspiration. Then think again.

If you get out of memory, you probably have a fundamental problem with your design. Most likely you are loading too much data at a time (try using iterators?), Or maybe your code should be better structured.

I just saw your edit. Do you need all this array in memory at the same time? Can you use a generator?

Another alternative is to use a database such as SQLite or perhaps shelve

0
source share

All Articles