memory leak

Hi,
I posted this on maplotlib-users list, but got no reply. I guess that bugs should rather be reported here...

I have noticed a memory leak when using pylab.pcolor. Here is the code,
fa() and fb() do the same thing. The difference is the size of the array
which is passed to pcolor. With a large array pcolor leaks but not with a
small one.
Cheers,
Ben

import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pylab
def fa():
     """ This function leaks.
     """
     a = np.arange(1024**2)
     a = a.reshape(1024,1024)
     for i in range(1):
         pylab.pcolor(a)
         pylab.close()
def fb():
     """This function does not leak.
     """
     b = np.arange(1024)
     b = b.reshape(32,32)
     for i in range(1024):
         pylab.pcolor(b)
         pylab.close()

Could you post a *complete* script that demonstrates the leak, eg one
that calls the function and does any other cleanup? Does it help to
use gc.collect between function calls?

JDH

···

On Fri, Nov 21, 2008 at 4:24 AM, Benoit Zuber <bzuber@...674...> wrote:

Hi,
I posted this on maplotlib-users list, but got no reply. I guess that
bugs should rather be reported here...

> I posted this on maplotlib-users list, but got no reply. I guess that
> bugs should rather be reported here...

Could you post a *complete* script that demonstrates the leak, eg one
that calls the function and does any other cleanup? Does it help to
use gc.collect between function calls?

Thanks for your reply. Here is the complete script (I was running the
previous one interactively).
In fact, I realised that the memory leak is not total... I mean that the
RAM gets loaded during the first two iterations, which correspond to a
load of 1.9Gb (I have 4Gb RAM in total). Then the RAM usage remains
absolutely stable.

I then tried to run this script interactively in ipython. Once the
script ends, the RAM is not released (1.9Gb are still used).
Nevertheless, when I call fa() once again, the memory load remains the
same. So this leak does not lead to a crash, which is fine.

Finally if I comment "matplotlib.use('Agg')", then the load is
increasing during each iteration, saturating the RAM, and starting
filling up the swap. In this case the output of the script is :
9
9
9
9
9

Cheers,
Ben

import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pylab
import gc
def fa():
    a = np.arange(1024**2)
    a = a.reshape(1024,1024)
    for i in range(5):
        filename = "memleak%d" %(i)
        pylab.pcolor(a)
        pylab.savefig(filename)
        pylab.close()
        print gc.collect()
fa()

This outputs:
0
0
0
0
0

> I posted this on maplotlib-users list, but got no reply. I guess that
> bugs should rather be reported here...

Could you post a *complete* script that demonstrates the leak, eg one
that calls the function and does any other cleanup? Does it help to
use gc.collect between function calls?

Thanks for your reply. Here is the complete script (I was running the
previous one interactively).
In fact, I realised that the memory leak is not total... I mean that the
RAM gets loaded during the first two iterations, which correspond to a
load of 1.9Gb (I have 4Gb RAM in total). Then the RAM usage remains
absolutely stable.

I then tried to run this script interactively in ipython. Once the
script ends, the RAM is not released (1.9Gb are still used).
Nevertheless, when I call fa() once again, the memory load remains the
same. So this leak does not lead to a crash, which is fine.

Finally if I comment "matplotlib.use('Agg')", then the load is
increasing during each iteration, saturating the RAM, and starting
filling up the swap. In this case the output of the script is :

If you comment out agg you are using a gui backend presumably (which
one) and most of these are known to have some leaks, some of which are
beyond our control. Michael has recently made some change to
significantly reduce a gtk leak.

When you say you are working interactively, do you mean from the
python or ipython shell? ipython holds a reference to the names the
main module namespace, which could be preventing a gc cleanup. After
reading your post, I am not clear if you still have a problem or not.

From the data you posted, it appears that agg is not leaking in your

example.

JDH

···

On Fri, Nov 21, 2008 at 7:04 AM, Benoit Zuber <bzuber@...674...> wrote:

If you comment out agg you are using a gui backend presumably (which
one) and most of these are known to have some leaks, some of which are
beyond our control.

This leak happened without any gui backend when I ran the script from the csh prompt like that:
> python script.py

When you say you are working interactively, do you mean from the
python or ipython shell?

Yes, from ipython shell.

After
reading your post, I am not clear if you still have a problem or not.>From the data you posted, it appears that agg is not leaking in your
example.
  

It is not a problem anymore, using the 'Agg' solved the problem.

Thanks.
Ben

If you comment out agg you are using a gui backend presumably (which
one) and most of these are known to have some leaks, some of which are
beyond our control.

This leak happened without any gui backend when I ran the script from the
csh prompt like that:

python script.py

Well, you are still using some backend, probably a GUI one, even if no
figure pops up. You can run your script with --verbose-helpful to see
what is happening.

After
reading your post, I am not clear if you still have a problem or not.>From
the data you posted, it appears that agg is not leaking in your
example.

It is not a problem anymore, using the 'Agg' solved the problem.

Great

JDH

···

On Fri, Nov 21, 2008 at 7:53 AM, Benoit Zuber <bzuber@...674...> wrote:

Well, you are still using some backend, probably a GUI one, even if no
figure pops up. You can run your script with --verbose-helpful to see
what is happening.
  
Sorry, I did not realise it (I suppose, I did not quite know what backend means, now I checked it on wikipedia :wink: . Running the script with --verbose-helpful told me that the backend was GTKAgg version 2.10.1

Best regards,
Ben

Here is some additional documentation of backends from the mpl perspective:

  http://matplotlib.sourceforge.net/faq/installing_faq.html#id1

JDH

···

On Fri, Nov 21, 2008 at 8:17 AM, Benoit Zuber <bzuber@...674...> wrote:

Sorry, I did not realise it (I suppose, I did not quite know what backend
means, now I checked it on wikipedia :wink: . Running the script with
--verbose-helpful told me that the backend was GTKAgg version 2.10.1