Python 2.4.3 array memory leak

Robert Kern robert.kern at
Thu May 7 18:57:04 EDT 2009

On 2009-05-07 16:31, danmcleran at wrote:
> I am using the array module to instantiate many arrays in my
> application. It seems like there is a memory leak in there somewhere.
> Can anyone confim this and let me know what, if anything, I can do
> about it? I am using Fedora Core 5 Linux:
> import commands
> import array
> import itertools
> import sys
> from itertools import repeat
> print '*** before ***'
> print commands.getoutput('cat /proc/meminfo').split('\n')[1]
> for i in range(100):
>      a = array.array('I', repeat(0, int(2E6)))
>      del a
> print '*** after ***'
> print commands.getoutput('cat /proc/meminfo').split('\n')[1]
> Output:
> *** before ***
> MemFree:       1459772 kB
> *** after ***
> MemFree:       1457688 kB

This is not necessarily indicative of a memory leak, per se. Python sometimes 
does not release memory back to the OS. The memory isn't leaked; it will be 
reused by Python for later allocations. Now, if you saw Python's memory usage 
*grow* with increasing iterations, then that would be evidence of a leak.

Try measuring the "before" memory, then do one iteration, measure the memory 
again (let's call it "intermediate"), then do your loop, and then measure the 
"after" memory. The "intermediate" measurement should be about your "after" 

Also, try to measure the process's own memory usage, not the system's free memory.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list