Python: is the "old" memory free'd when a variable is assigned new content?

Go To StackoverFlow.com

5

If a variable is assigned any new content, will the memory allocated for the "old content" be "properly" free'd? For example, in the following script, will the memory for variable "a" as an array of zeros be free'd after "a" is assigned some new stuff

import numpy
a = numpy.zeros(1000)
a = a+1

I would imaging Python is smart enough to do everything cleanly, using the so-called 'garbage collection', which I never really be able to read through. Any confirmation? I'd appreciate it.

2012-04-04 21:21
by Liang
this question might help as well - Joel Cornett 2012-04-04 21:34


8

Eventually, the old memory will be freed, though you cannot predict when this will happen. It is dependent on the Python implementation and many other factors.

That said, for the example you gave and the CPython implementation, the old array should be garbage collected during the assignment.

(Note that NumPy arrays are a particularly complex example for discussing garbage-collector behaviour.)

2012-04-04 21:24
by Sven Marnach
Thank you Sven. The problem for me is that sometimes I need to operate big matrices. In consideration of memory and speed, I always wrap things in functions, hoping that at least any extra memory allocated in a function will be free'd at end of THE function. Is that a good way to avoid bad things? BTW, I do use NumPy arrays mainly, is there a good alternative regarding both performance and convenience - Liang 2012-04-04 21:55
@Liang: There's no reasonable alternative to NumPy arrays. If you want to avoid excessive memory usage, try to use in-place operations wherever possible, and follow the recommendations in this excellent answer; be sure to also have a look at the numexpr module linked there. For the code in your question, this would mean to use a += 1 to avoid any temporary arrays in the first place - Sven Marnach 2012-04-04 22:07
There's also a difference between when an object's memory can be reused by another Python object, and when the memory is released back to the OS. CPython uses some specialized structures for particular data types that keep this from happening entirely in some cases, and fragmentation may keep it from happening in other cases - kindall 2012-04-04 22:16


1

You can find the answer by playing with gc module (and probably finetuning). It provides the ability to disable the collector, tune the collection frequency, and set debugging options. It also provides access to unreachable objects that the collector found but cannot free. See http://docs.python.org/library/gc.html

2012-04-04 21:36
by Maksym Polshcha
Thank you Mark. I will try it out. It looks formidable for me, though. Actually I was wandering is there a "general" principle, like, "wrapping thing in functions". Thank you for the suggestion - Liang 2012-04-04 22:00
Ads