The Python Oracle

Static memory in python: do loops create new instances of variables in memory?

This video explains
Static memory in python: do loops create new instances of variables in memory?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Question
01:13 Accepted answer (Score 17)
02:05 Answer 2 (Score 2)
02:47 Answer 3 (Score 0)
03:23 Thank you

--

Full question
https://stackoverflow.com/questions/3305...

Answer 1 links:
[How do I pass a variable by reference?]: https://stackoverflow.com/questions/9860...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #memory

#avk47



ACCEPTED ANSWER

Score 17


Without the del y you might need twice as much memory. This is because for each pass through the loop, y is bound to the previous value of F1 while the next one is calculated.

once F1 returns y is rebound to that new value and the old F1 result can be released.

This would mean that the object returned by F1 occupies quite a lot of memory

Unrolling the loop for the first couple of iterations would look like this

y = F1(x[0])   # F1(x[0]) is calculated, then y is bound to it
z[j] = F2(y)
y = F1(x[1])   # y is still bound to F1(x[0]) while F1(x[1]) is computed
               # The memory for F1(X[0]) is finally freed when y is rebound
z[j] = F2(y)

using del y is a good solution if this is what is happening in your case.




ANSWER 2

Score 2


what you actually want is something that's weird to do in python -- you want to allocate a region of memory for y and pass the pointer to that region to F1() so it can use that region to build up the next value of y. this avoid having F1() do it's own allocation for the new value of y, the reference to which is then written into your own variable y (which is actually not the value of whatever F1() calculated but a reference to it)

There's already an SO question about passing by reference in python: How do I pass a variable by reference?




ANSWER 3

Score 0


For very large values of N use xrange instead of range for memory save. Also you can nest functions but I don't know if this will help you. : \

x = LoadData()

for j in xrange(N):
    z[j] = F2(F1(x[j]))

SaveData(z)

Maybe F1 and F2 are making unnecessary copies of objects, the best way would be in-place, something like:

x = LoadData()
for item in x:
    item.F1()
    item.F2()
SaveData(x)

Sorry if may answer is not helpful