Memory leak when using pyplot.ion() ?

Hi,

I am having problems with a script. It runs a number of iterations and plots and saves a number of plots on each iteration. After the plots have been saved I issue the pyplot.close(‘all’) command so despite many plots
being created only 4 should be open at any given time which should not cause any memory problems. When I run the script however I see the RAM usage gradually growing without bound and eventually causing the script to crash. Interestingly I have found if I
comment out the pyplot.ion() and pyplot.ioff() the problem vanishes. So I do have a workaround but it would still be good to have this fixed in case I forget about it in future and loose another weekend’s work.

My OS is Windows XP Service Pack 3

Python 2.6

Matplotlib 1.0.1

The code below is a stripped down version of my script which still exhibits the problem.

Oisín.

-- coding: utf-8 --

import sys

import time

import numpy as np

from matplotlib import pyplot

import os

Main script body

try:

for gain in range(1,20,2):

for PortToTest in range(8):

dirname = ‘.\crash’

f = open(dirname + ‘\results.m’,‘w’)

runname = ‘\P’ + str(PortToTest) + str(gain) + \

‘_’ + time.strftime(‘d%dh%Hm%Ms%S’)

dirname = dirname + runname

os.mkdir(dirname)

os.system('copy ’ + sys.argv[0] + ’ ’ + dirname )

nIts = 50

Decimate data for plotting if many iterations are run

if(nIts>10):

echoPlotDec = 10

else:

echoPlotDec = 1

ResidN = np.zeros((4,2*nIts))

MaxSl = np.zeros((4,2*nIts))

MaxOld = np.zeros((4,2*nIts))

MaxNew = np.zeros((4,2*nIts))

EchoA = np.zeros((2*nIts,160))

for kk in range(2*nIts):

ResidN[0,kk] = np.random.rand(1,1)

ResidN[1,kk] = np.random.rand(1,1)

ResidN[2,kk] = np.random.rand(1,1)

ResidN[3,kk] = np.random.rand(1,1)

MaxSl[0,kk] = np.random.rand(1,1)

MaxSl[1,kk] = np.random.rand(1,1)

MaxSl[2,kk] = np.random.rand(1,1)

MaxSl[3,kk] = np.random.rand(1,1)

MaxOld[0,kk] = np.random.rand(1,1)

MaxOld[1,kk] = np.random.rand(1,1)

MaxOld[2,kk] = np.random.rand(1,1)

MaxOld[3,kk] = np.random.rand(1,1)

MaxNew[0,kk] = np.random.rand(1,1)

MaxNew[1,kk] = np.random.rand(1,1)

MaxNew[2,kk] = np.random.rand(1,1)

MaxNew[3,kk] = np.random.rand(1,1)

EchoA[kk,:] = np.random.rand(1,160)

f.close()

pyplot.ion()

pyplot.figure()

pyplot.hold(True)

LegendTexts = (“A”,“B”,“C”,“D”)

pyplot.title(“R (” + runname +")")

pyplot.xlabel(“Index”)

pyplot.ylabel(“Noise (dB)”)

pyplot.grid(True)

pyplot.hold(True)

pyplot.plot(np.transpose(ResidN),’.-’)

pyplot.legend(LegendTexts,loc=1)

pyplot.axis([0, 2*nIts, -33, -25])

pyplot.savefig(dirname + ‘\results.emf’,format=‘emf’)

pyplot.figure()

pyplot.hold(True)

pyplot.title(“Coefs”)

pyplot.xlabel(“Coef Index”)

pyplot.ylabel(“Coef Value”)

pyplot.grid(True)

pyplot.hold(True)

pyplot.plot(np.transpose(EchoA[0:nIts-1:echoPlotDec,:]),’.-’)

pyplot.plot(np.transpose(EchoA[nIts:2nIts-1:echoPlotDec,:]),’-’)

pyplot.axis([0, 160, -0.5, 2])

pyplot.savefig(dirname + ‘\CoefsA.emf’,format=‘emf’)

pyplot.figure()

pyplot.hold(True)

pyplot.title("MaxAbs, Old = ‘.’, New = ‘*’ ")

pyplot.xlabel(“Iteration”)

pyplot.ylabel(“o/p (LSBs)”)

pyplot.grid(True)

pyplot.hold(True)

pyplot.plot(np.transpose(MaxOld),’.-’)

pyplot.plot(np.transpose(MaxNew),’*-’)

pyplot.axis([0, 2*nIts, 32, 128])

pyplot.savefig(dirname + ‘\MaxAbsA.emf’,format=‘emf’)

pyplot.figure()

pyplot.hold(True)

pyplot.title(“MaxAbs”)

pyplot.xlabel(“Iteration”)

pyplot.ylabel("(LSBs)")

pyplot.grid(True)

pyplot.hold(True)

pyplot.plot(np.transpose(MaxSl),’.-’)

pyplot.axis([0, 2*nIts, 0, 64])

pyplot.savefig(dirname + ‘\MaxAbsSl.emf’,format=‘emf’)

pyplot.close(‘all’)

except RuntimeError, msg:

print ‘Exception occurred in main script body’

print >>sys.stderr, msg

raise

finally:

print “Test done”

Display plots

pyplot.ioff()

I haven't had a chance to look into where the memory is actually leaking, ion/ioff are intended for interactive use, and here you are saving a large number of plots to files. Why do you need ion at all?

Mike

···

On 10/14/2013 08:51 AM, OCuanachain, Oisin (Oisin) wrote:

Hi,

I am having problems with a script. It runs a number of iterations and plots and saves a number of plots on each iteration. After the plots have been saved I issue the pyplot.close('all') command so despite many plots being created only 4 should be open at any given time which should not cause any memory problems. When I run the script however I see the RAM usage gradually growing without bound and eventually causing the script to crash. Interestingly I have found if I comment out the pyplot.ion() and pyplot.ioff() the problem vanishes. So I do have a workaround but it would still be good to have this fixed in case I forget about it in future and loose another weekend's work.

My OS is Windows XP Service Pack 3

Python 2.6

Matplotlib 1.0.1

The code below is a stripped down version of my script which still exhibits the problem.

Ois�n.

# -*- coding: utf-8 -*-

import sys

import time

import numpy as np

from matplotlib import pyplot

import os

  # Main script body

try:

  for gain in range(1,20,2):

      for PortToTest in range(8):

        dirname = '.\crash'

        f = open(dirname + '\\results.m','w')

        runname = '\P' + str(PortToTest) + str(gain) + \

                  '_' + time.strftime('d%dh%Hm%Ms%S')

        dirname = dirname + runname

        os.mkdir(dirname)

        os.system('copy ' + sys.argv[0] + ' ' + dirname )

        nIts = 50

        # Decimate data for plotting if many iterations are run

        if(nIts>10):

          echoPlotDec = 10

        else:

          echoPlotDec = 1

        ResidN = np.zeros((4,2*nIts))

        MaxSl = np.zeros((4,2*nIts))

        MaxOld = np.zeros((4,2*nIts))

        MaxNew = np.zeros((4,2*nIts))

        EchoA = np.zeros((2*nIts,160))

        for kk in range(2*nIts):

            ResidN[0,kk] = np.random.rand(1,1)

            ResidN[1,kk] = np.random.rand(1,1)

            ResidN[2,kk] = np.random.rand(1,1)

            ResidN[3,kk] = np.random.rand(1,1)

            MaxSl[0,kk] = np.random.rand(1,1)

            MaxSl[1,kk] = np.random.rand(1,1)

            MaxSl[2,kk] = np.random.rand(1,1)

            MaxSl[3,kk] = np.random.rand(1,1)

            MaxOld[0,kk] = np.random.rand(1,1)

            MaxOld[1,kk] = np.random.rand(1,1)

            MaxOld[2,kk] = np.random.rand(1,1)

            MaxOld[3,kk] = np.random.rand(1,1)

            MaxNew[0,kk] = np.random.rand(1,1)

            MaxNew[1,kk] = np.random.rand(1,1)

            MaxNew[2,kk] = np.random.rand(1,1)

            MaxNew[3,kk] = np.random.rand(1,1)

            EchoA[kk,:] = np.random.rand(1,160)

        f.close()

        pyplot.ion()

        pyplot.figure()

        pyplot.hold(True)

        LegendTexts = ("A","B","C","D")

        pyplot.title("R (" + runname +")")

pyplot.xlabel("Index")

pyplot.ylabel("Noise (dB)")

        pyplot.grid(True)

        pyplot.hold(True)

pyplot.plot(np.transpose(ResidN),'.-')

pyplot.legend(LegendTexts,loc=1)

        pyplot.axis([0, 2*nIts, -33, -25])

pyplot.savefig(dirname + '\\results.emf',format='emf')

        pyplot.figure()

        pyplot.hold(True)

        pyplot.title("Coefs")

pyplot.xlabel("Coef Index")

pyplot.ylabel("Coef Value")

        pyplot.grid(True)

        pyplot.hold(True)

pyplot.plot(np.transpose(EchoA[0:nIts-1:echoPlotDec,:]),'.-')

pyplot.plot(np.transpose(EchoA[nIts:2*nIts-1:echoPlotDec,:]),'*-')

        pyplot.axis([0, 160, -0.5, 2])

pyplot.savefig(dirname + '\\CoefsA.emf',format='emf')

        pyplot.figure()

        pyplot.hold(True)

        pyplot.title("MaxAbs, Old = '.', New = '*' ")

pyplot.xlabel("Iteration")

pyplot.ylabel("o/p (LSBs)")

        pyplot.grid(True)

        pyplot.hold(True)

pyplot.plot(np.transpose(MaxOld),'.-')

pyplot.plot(np.transpose(MaxNew),'*-')

        pyplot.axis([0, 2*nIts, 32, 128])

pyplot.savefig(dirname + '\\MaxAbsA.emf',format='emf')

        pyplot.figure()

        pyplot.hold(True)

        pyplot.title("MaxAbs")

pyplot.xlabel("Iteration")

pyplot.ylabel("(LSBs)")

        pyplot.grid(True)

        pyplot.hold(True)

pyplot.plot(np.transpose(MaxSl),'.-')

        pyplot.axis([0, 2*nIts, 0, 64])

pyplot.savefig(dirname + '\\MaxAbsSl.emf',format='emf')

        pyplot.close('all')

except RuntimeError, msg:

  print 'Exception occurred in main script body'

  print >>sys.stderr, msg

  raise

finally:

  print "Test done"

  # Display plots

  pyplot.ioff()

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
                    _

\/|o _|_ _. _ | | \.__ __|__|_|_ _ _ ._ _
>>(_| |(_|(/_| |_/|(_)(/_|_ |_|_)(_)(_)| | |

Hi Mike,

ion(), ioff() are useful to get immediate feedback when developing a script, when it is fully debugged I then increase the number of iterations and leave it running over the weekend. At that point I could obviously
also have removed ion(), ioff() but given that I had no idea that this was necessary my sim crashed and I lost a weekend’s worth of sim time. Anyway, whether or not ion(),ioff() are needed in this particular script is really besides the point. If the script,
however unusual, is revealing a bug in matplotlib it should be logged so that it can hopefully be fixed.

Oisín

···

From: Michael Droettboom [mailto:mdroe@…86…]
Sent: 14 October 2013 18:13
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Memory leak when using pyplot.ion() ?

I haven’t had a chance to look into where the memory is actually leaking, ion/ioff are intended for interactive use, and here you are saving a large number of plots to files. Why do you need ion at all?

Mike

On 10/14/2013 08:51 AM, OCuanachain, Oisin (Oisin) wrote:

Hi,

I am having problems with a script. It runs a number of iterations and plots and saves a number of plots on each iteration. After the plots have been saved I issue the pyplot.close(‘all’) command so despite many plots
being created only 4 should be open at any given time which should not cause any memory problems. When I run the script however I see the RAM usage gradually growing without bound and eventually causing the script to crash. Interestingly I have found if I
comment out the pyplot.ion() and pyplot.ioff() the problem vanishes. So I do have a workaround but it would still be good to have this fixed in case I forget about it in future and loose another weekend’s work.

My OS is Windows XP Service Pack 3

Python 2.6

Matplotlib 1.0.1

The code below is a stripped down version of my script which still exhibits the problem.

Oisín.

-- coding: utf-8 --

import sys

import time

import numpy as np

from matplotlib import pyplot

import os

Main script body

try:

for gain in range(1,20,2):

  for PortToTest in range(8): 
    dirname = '.\crash'
    f = open(dirname + '\\results.m','w') 
    runname = '\P' + str(PortToTest) + str(gain) + \
              '_' + time.strftime('d%dh%Hm%Ms%S')
    dirname = dirname + runname
    os.mkdir(dirname)
    os.system('copy ' + sys.argv[0] + ' ' + dirname )
    nIts     = 50
    # Decimate  data for plotting if many iterations are run
    if(nIts>10):
      echoPlotDec = 10
    else:
      echoPlotDec = 1
    ResidN   = np.zeros((4,2*nIts))
    MaxSl    = np.zeros((4,2*nIts))
    MaxOld   = np.zeros((4,2*nIts))
    MaxNew   = np.zeros((4,2*nIts))
    EchoA    = np.zeros((2*nIts,160)) 
    for kk in range(2*nIts):           
        ResidN[0,kk] = np.random.rand(1,1)
        ResidN[1,kk] = np.random.rand(1,1)
        ResidN[2,kk] = np.random.rand(1,1)
        ResidN[3,kk] = np.random.rand(1,1)
        MaxSl[0,kk] = np.random.rand(1,1)
        MaxSl[1,kk] = np.random.rand(1,1)
        MaxSl[2,kk] = np.random.rand(1,1)
        MaxSl[3,kk] = np.random.rand(1,1)
        MaxOld[0,kk] = np.random.rand(1,1)
        MaxOld[1,kk] = np.random.rand(1,1)
        MaxOld[2,kk] = np.random.rand(1,1)
        MaxOld[3,kk] = np.random.rand(1,1)
        MaxNew[0,kk] = np.random.rand(1,1)
        MaxNew[1,kk] = np.random.rand(1,1)
        MaxNew[2,kk] = np.random.rand(1,1)
        MaxNew[3,kk] = np.random.rand(1,1)
        EchoA[kk,:] = np.random.rand(1,160)
    f.close()
    pyplot.ion()
    pyplot.figure()
    pyplot.hold(True)
    LegendTexts = ("A","B","C","D")
    pyplot.title("R (" + runname +")")
    pyplot.xlabel("Index")
    pyplot.ylabel("Noise (dB)")
    pyplot.grid(True)
    pyplot.hold(True)
    pyplot.plot(np.transpose(ResidN),'.-')
    pyplot.legend(LegendTexts,loc=1)
    pyplot.axis([0, 2*nIts, -33, -25])
    pyplot.savefig(dirname + '\\results.emf',format='emf')
    pyplot.figure()
    pyplot.hold(True)
    pyplot.title("Coefs")
    pyplot.xlabel("Coef Index")
    pyplot.ylabel("Coef Value")
    pyplot.grid(True)
    pyplot.hold(True)
    pyplot.plot(np.transpose(EchoA[0:nIts-1:echoPlotDec,:]),'.-')
    pyplot.plot(np.transpose(EchoA[nIts:2*nIts-1:echoPlotDec,:]),'*-')
    pyplot.axis([0, 160, -0.5, 2])
    pyplot.savefig(dirname + '\\CoefsA.emf',format='emf')
    pyplot.figure()
    pyplot.hold(True)
    pyplot.title("MaxAbs, Old = '.', New = '*' ")
    pyplot.xlabel("Iteration")
    pyplot.ylabel("o/p (LSBs)")
    pyplot.grid(True)
    pyplot.hold(True)
    pyplot.plot(np.transpose(MaxOld),'.-')
    pyplot.plot(np.transpose(MaxNew),'*-')
    pyplot.axis([0, 2*nIts, 32, 128])
    pyplot.savefig(dirname + '\\MaxAbsA.emf',format='emf')
    pyplot.figure()
    pyplot.hold(True)
    pyplot.title("MaxAbs")
    pyplot.xlabel("Iteration")
    pyplot.ylabel("(LSBs)")
    pyplot.grid(True)
    pyplot.hold(True)
    pyplot.plot(np.transpose(MaxSl),'.-')
    pyplot.axis([0, 2*nIts, 0, 64])
    pyplot.savefig(dirname + '\\MaxAbsSl.emf',format='emf')
    pyplot.close('all')

except RuntimeError, msg:

print ‘Exception occurred in main script body’

print >>sys.stderr, msg

raise

finally:

print “Test done”

Display plots

pyplot.ioff()

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from
the latest Intel processors and coprocessors. See abstracts and register >
[http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk](http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk)
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
[https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)
--

_

|\/|o _|_  _. _ | | \.__  __|__|_|_  _  _ ._ _

| ||(| |(|(/| |/|()(/|_ ||)()()| | |


http://www.droettboom.com

Is this actually a matplotlib problem or could it be a Windows problem as discussed here http://bugs.python.org/issue19246 ?

···

On 14/10/2013 13:51, OCuanachain, Oisin (Oisin) wrote:

Hi,

I am having problems with a script. It runs a number of iterations and
plots and saves a number of plots on each iteration. After the plots
have been saved I issue the pyplot.close(�all�) command so despite many
plots being created only 4 should be open at any given time which should
not cause any memory problems. When I run the script however I see the
RAM usage gradually growing without bound and eventually causing the
script to crash. Interestingly I have found if I comment out the
pyplot.ion() and pyplot.ioff() the problem vanishes. So I do have a
workaround but it would still be good to have this fixed in case I
forget about it in future and loose another weekend�s work.

My OS is Windows XP Service Pack 3
Python 2.6
Matplotlib 1.0.1

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

I see you are using matplotlib 1.0.1. There have been several memory leak bugs fixed since then, so I would suggest upgrading. I also notice you are using the “emf” backend for saving figures. If I remember correctly, that backend has been deprecated (or maybe even removed) in the latest release (v1.3.x). So, i would suggest trying v1.2.1. It does contain many bugfixes and should still be compatible with your existing code.

I should also warn that the behavior with pyplot.ion() was “odd” back in the 1.0.1 days and prior. If you are just simply running the script as-is with a more recent matplotlib, or with a different backend in v1.0.1, the script might behave a little differently than you’d expect. We would welcome feedback on your usage of pyplot.ion().

Cheers!
Ben Root

Ois�n,

Certainly we want to find and fix bugs, with memory leaks being high priority. (I don't think we have seen a genuine mpl memory leak for quite a while; I am not aware of any at present.) We are not trying to maintain old mpl versions such as 1.0.1, however. You are using the emf backend, which has been removed. Therefore, unless you can reproduce the problem with mpl 1.3.x or 1.4.x in a SSCCE (http://sscce.org/), it is unlikely that your report will lead to a bug fix. Perhaps it will lead to some useful insight, however.

I see someone has suggested that the problem might be in Windows. Another possibility is that it is in TkAgg, which I suspect is your default interactive backend. I dimly recall that there was a time when TkAgg could leak memory, but I don't remember whether that was fixed by 1.0.1 or not.

Eric

···

On 2013/10/14 7:48 AM, OCuanachain, Oisin (Oisin) wrote:

Hi Mike,

ion(), ioff() are useful to get immediate feedback when developing a
script, when it is fully debugged I then increase the number of
iterations and leave it running over the weekend. At that point I could
obviously also have removed ion(), ioff() but given that I had no idea
that this was necessary my sim crashed and I lost a weekend�s worth of
sim time. Anyway, whether or not ion(),ioff() are needed in this
particular script is really besides the point. If the script, however
unusual, is revealing a bug in matplotlib it should be logged so that it
can hopefully be fixed.

Ois�n

-----Bunteachtaireacht-----

···

From: Eric Firing [mailto:efiring@…202…]
Sent: 14 October 2013 19:09
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Maidir le: Memory leak when using pyplot.ion() ?

On 2013/10/14 7:48 AM, OCuanachain, Oisin (Oisin) wrote:

Hi Mike,

ion(), ioff() are useful to get immediate feedback when developing a
script, when it is fully debugged I then increase the number of
iterations and leave it running over the weekend. At that point I
could obviously also have removed ion(), ioff() but given that I had
no idea that this was necessary my sim crashed and I lost a weekend's
worth of sim time. Anyway, whether or not ion(),ioff() are needed in
this particular script is really besides the point. If the script,
however unusual, is revealing a bug in matplotlib it should be logged
so that it can hopefully be fixed.

Oisín

Oisín,

Certainly we want to find and fix bugs, with memory leaks being high priority. (I don't think we have seen a genuine mpl memory leak for quite a while; I am not aware of any at present.) We are not trying to maintain old mpl versions such as 1.0.1, however. You are using the emf backend, which has been removed. Therefore, unless you can reproduce the problem with mpl 1.3.x or 1.4.x in a SSCCE (http://sscce.org/), it is unlikely that your report will lead to a bug fix. Perhaps it will lead to some useful insight, however.

I see someone has suggested that the problem might be in Windows.
Another possibility is that it is in TkAgg, which I suspect is your default interactive backend. I dimly recall that there was a time when TkAgg could leak memory, but I don't remember whether that was fixed by
1.0.1 or not.

Eric

Hi Eric,

If .emf is no longer supported in current versions of matplotlib is there an alternative SVG-type format I can use ? I use .emf because I find that it tends to produce the clearest plots independent of how I re-size them when imported into documents. If memory serves I used to have a lot of problems with legibility when using raster-type formats like .png. If I get a chance I will try installing an up-to-date version of matplotlib and see if I can re-produce the behaviour.

Oisin.

Oisin,

The ps, pdf, and svg vector formats are fully supported. (Postscript inherently lacks support for transparency, though, so pdf and svg are recommended.)

Eric

···

On 2013/10/14 8:26 AM, OCuanachain, Oisin (Oisin) wrote:

Hi Eric,

If .emf is no longer supported in current versions of matplotlib is
there an alternative SVG-type format I can use ? I use .emf because I
find that it tends to produce the clearest plots independent of how I
re-size them when imported into documents. If memory serves I used to
have a lot of problems with legibility when using raster-type formats
like .png. If I get a chance I will try installing an up-to-date
version of matplotlib and see if I can re-produce the behaviour.

Oisin.

2013/10/14 Mark Lawrence <breamoreboy@...225...>:

···

On 14/10/2013 13:51, OCuanachain, Oisin (Oisin) wrote:

Hi,

I am having problems with a script. It runs a number of iterations and
plots and saves a number of plots on each iteration. After the plots
have been saved I issue the pyplot.close(‘all’) command so despite many
plots being created only 4 should be open at any given time which should
not cause any memory problems. When I run the script however I see the
RAM usage gradually growing without bound and eventually causing the
script to crash. Interestingly I have found if I comment out the
pyplot.ion() and pyplot.ioff() the problem vanishes. So I do have a
workaround but it would still be good to have this fixed in case I
forget about it in future and loose another weekend’s work.

My OS is Windows XP Service Pack 3
Python 2.6
Matplotlib 1.0.1

Is this actually a matplotlib problem or could it be a Windows problem
as discussed here http://bugs.python.org/issue19246 ?

I think this is different. That bug report is not about RAM usage
growing without bound but memory allocation failing with plenty of RAM
available.

Goyo