Qt4/PySide memory leak

I’ve run into a large memory leak using Matplotlib with PySide and the Qt4 back end. I’m using :

Python 3.2

Numpy 1.6.2

Pyside 1.1.1 (qt474)

Matplotlib 1.2 (first the Capetown Group port to Python 3, then 1.2 RC2)

on Windows XP 32 bit

I’ve tried using the Python 2.7 branch of all of the above and don’t see the problem. I don’t see the problem with the Tk back end. I don’t see the problem with the Qt4 back end and PyQt4. Only with the above mentioned versions and using the Qt4 back end with PySide.

The following script will reproduce the problem :

···

import matplotlib

matplotlib.use(‘Qt4Agg’)
matplotlib.rcParams[‘backend.qt4’]=‘PySide’

import pylab

arrayX = []
arrayY = []
for nIndex in range(0, 100):
arrayX.append(nIndex)
arrayY.append(nIndex)

Figure = matplotlib.pyplot.figure(1)
Axes = Figure.add_axes([ 0.05, 0.05, 0.95, 0.95])

Axes.plot(arrayX,
arrayY,
color = “blue”,
marker = “o”,
markersize = 5.0)

Axes.set_xlim(arrayX[0], arrayX[len(arrayX) - 1])
Axes.set_ylim(arrayY[0], arrayY[len(arrayY) - 1])


I run the above, grab the lower right sizing handle on the plot window and start resizing the window. Watching the python process in task manager, each resize leaks a noticeable amount of memory. A few minutes of this will get process memory up to ~2.5 GB. At that point it crashes.

I’m new here, am I in the right place?

matplotlib.pyplot.show()

I can reproduce this exactly, also with pyside 1.1.2 and an empty plot. Looks like QtGui.QImage is leaking.

import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib import pyplot
pyplot.plot()
pyplot.show()

···

On 9/24/2012 3:32 PM, David Honcik wrote:

I've run into a large memory leak using Matplotlib with PySide and the
Qt4 back end. I'm using :
Python 3.2
Numpy 1.6.2
Pyside 1.1.1 (qt474)
Matplotlib 1.2 (first the Capetown Group port to Python 3, then 1.2 RC2)
on Windows XP 32 bit
I've tried using the Python 2.7 branch of all of the above and don't see
the problem. I don't see the problem with the Tk back end. I don't see
the problem with the Qt4 back end and PyQt4. Only with the above
mentioned versions and using the Qt4 back end with PySide.
The following script will reproduce the problem :
--------------------
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
import pylab
arrayX =
arrayY =
for nIndex in range(0, 100):
arrayX.append(nIndex)
arrayY.append(nIndex)
Figure = matplotlib.pyplot.figure(1)
Axes = Figure.add_axes([ 0.05, 0.05, 0.95, 0.95])
Axes.plot(arrayX,
arrayY,
color = "blue",
marker = "o",
           markersize = 5.0)
Axes.set_xlim(arrayX[0], arrayX[len(arrayX) - 1])
Axes.set_ylim(arrayY[0], arrayY[len(arrayY) - 1])
matplotlib.pyplot.show()
--------------------
I run the above, grab the lower right sizing handle on the plot window
and start resizing the window. Watching the python process in task
manager, each resize leaks a noticeable amount of memory. A few minutes
of this will get process memory up to ~2.5 GB. At that point it crashes.
I'm new here, am I in the right place?

--
Christoph

As a workaround, until PySide is fixed, use the attached patch, which forcibly decreases the reference count of the string buffer after use.

Christoph

mpl-pyside-leak.diff (1.59 KB)

···

On 9/24/2012 5:50 PM, Christoph Gohlke wrote:

On 9/24/2012 3:32 PM, David Honcik wrote:

I've run into a large memory leak using Matplotlib with PySide and the
Qt4 back end. I'm using :
Python 3.2
Numpy 1.6.2
Pyside 1.1.1 (qt474)
Matplotlib 1.2 (first the Capetown Group port to Python 3, then 1.2 RC2)
on Windows XP 32 bit
I've tried using the Python 2.7 branch of all of the above and don't see
the problem. I don't see the problem with the Tk back end. I don't see
the problem with the Qt4 back end and PyQt4. Only with the above
mentioned versions and using the Qt4 back end with PySide.
The following script will reproduce the problem :
--------------------
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
import pylab
arrayX =
arrayY =
for nIndex in range(0, 100):
arrayX.append(nIndex)
arrayY.append(nIndex)
Figure = matplotlib.pyplot.figure(1)
Axes = Figure.add_axes([ 0.05, 0.05, 0.95, 0.95])
Axes.plot(arrayX,
arrayY,
color = "blue",
marker = "o",
            markersize = 5.0)
Axes.set_xlim(arrayX[0], arrayX[len(arrayX) - 1])
Axes.set_ylim(arrayY[0], arrayY[len(arrayY) - 1])
matplotlib.pyplot.show()
--------------------
I run the above, grab the lower right sizing handle on the plot window
and start resizing the window. Watching the python process in task
manager, each resize leaks a noticeable amount of memory. A few minutes
of this will get process memory up to ~2.5 GB. At that point it crashes.
I'm new here, am I in the right place?

I can reproduce this exactly, also with pyside 1.1.2 and an empty plot.
Looks like QtGui.QImage is leaking.

import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib import pyplot
pyplot.plot()
pyplot.show()

--
Christoph