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 tha
Write stuff 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 cause an exception