Python 2.4.3 array memory leak

danmcleran at yahoo.com danmcleran at yahoo.com
Thu May 7 18:12:32 EDT 2009


On May 7, 3:58 pm, "danmcle... at yahoo.com" <danmcle... at yahoo.com>
wrote:
> On May 7, 3:47 pm, "danmcle... at yahoo.com" <danmcle... at yahoo.com>
> wrote:
>
>
>
> > On May 7, 3:31 pm, "danmcle... at yahoo.com" <danmcle... at yahoo.com>
> > 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
>
> > I hate to reply to my own thread but I wanted to update it. I tried
> > the same code using ctypes arrays and see no memory leak:
>
> > import commands
> > import array
> > import itertools
> > import sys
> > import ctypes
>
> > from itertools import repeat
> > from ctypes import *
>
> > print '*** before ***'
> > print commands.getoutput('cat /proc/meminfo').split('\n')[1]
> > for i in range(100):
> >     a = ARRAY(c_uint, int(2E6))
> >     del a
> > print '*** after ***'
> > print commands.getoutput('cat /proc/meminfo').split('\n')[1]
>
> > *** before ***
> > MemFree:       1416364 kB
> > *** after ***
> > MemFree:       1416364 kB
>
> The above code was not correct. I actually do see a memory leak when
> using ctypes arrays also. What is going on?
>
> import commands
> import array
> import itertools
> import sys
> import ctypes
>
> from itertools import repeat
> from ctypes import *
>
> print '*** before ***'
> print commands.getoutput('cat /proc/meminfo').split('\n')[1]
> for i in range(100):
>     a = ARRAY(c_uint, int(2E6))()
>     del a
> print '*** after ***'
> print commands.getoutput('cat /proc/meminfo').split('\n')[1]
>
> Output
> *** before ***
> MemFree:       1564096 kB
> *** after ***
> MemFree:       1556884 kB


Maybe the problem is with the way I was looking at free memory. I
changed my code to look at virtual memory allocated to the process in
question and I do not see any appreciable before/after difference.

import commands
import array
import itertools
import sys
import os

from itertools import repeat

deltas = []
print '*** before ***'
s = commands.getoutput('cat /proc/%d/status' % os.getpid()).split('\n')
[12]
print s
for i in range(100):
    a = array.array('I', repeat(0, int(2E6)))
    del a

print '*** after ***'
s = commands.getoutput('cat /proc/%d/status' % os.getpid()).split('\n')
[12]
print s

Output:

*** before ***
VmSize:	    5952 kB
*** after ***
VmSize:	    5956 kB



More information about the Python-list mailing list