Given the following code:
import numpy as np
import psutil
original = np.ones(2**28) / 2
def func1(x):
print(1,psutil.virtual_memory().percent)
x=x**2
print(2,psutil.virtual_memory().percent)
func2(x)
def func2(x):
print(3,psutil.virtual_memory().percent)
y=x**2
print(4,psutil.virtual_memory().percent)
del x # this does not lower the memory usage
print(5,psutil.virtual_memory().percent)
return
func1(original)
Would it be possible alter func1 in order to lower the memory consumption after del x in func? (since variable passed to func2 is no longer usefull to func1).
For instance, the following does not work:
import numpy as np
import psutil
original = np.ones(2**28) / 2
def func1(x):
print(1,psutil.virtual_memory().percent)
x=x**2
print(2,psutil.virtual_memory().percent)
x=[x]
func2(x.pop())
def func2(x):
print(3,psutil.virtual_memory().percent)
y=x**2
print(4,psutil.virtual_memory().percent)
del x # this does not lower the memory usage
print(5,psutil.virtual_memory().percent)
return
func1(x)
Note: assume func2 is external code that cannot be changed.
To put in another way, I would like that func1 passed x to func2 and deleted its internal reference to x before the begging of func2.
func1still has its own reference tox, and will continue to do so untilfunc2()returns. Nothing you can conceivably do insidefunc2can remove that reference.resize()to reduce the size of the array in place.func2, otherwise I could pass x encapsulated in a list and remove it from the list insidefunc2.