I know that python has an automatic garbage collector and so it should automatically delete variables when there are no more reference 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 and y still available here?
return y0, x0
Is del x
the right 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]
del y
return y0, x0
EDIT: I have edited my example such that it is more similar to my real problem.
In my real problem x and y are not lists but classes that contain different large np.array
.
EDIT: I am able to run the code:
x = f(z)
x0 = x[0]
print(x0)
y = f(z + 1)
y0 = [0]
print(y0)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…