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



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

Christoph
diff --git a/lib/matplotlib/backends/backend_qt4agg.py 
b/lib/matplotlib/backends/backend_qt4agg.py
index 0701781..408f3ce 100644
--- a/lib/matplotlib/backends/backend_qt4agg.py
+++ b/lib/matplotlib/backends/backend_qt4agg.py
@@ -4,6 +4,7 @@ Render to qt from agg
 from __future__ import division, print_function
 
 import os, sys
+import ctypes
 
 import matplotlib
 from matplotlib.figure import Figure
@@ -15,6 +16,10 @@ from backend_qt4 import QtCore, QtGui, FigureManagerQT, 
FigureCanvasQT,\
 
 DEBUG = False
 
+decref = ctypes.pythonapi.Py_DecRef
+decref.argtypes = [ctypes.py_object]
+decref.restype = None
+
 
 def new_figure_manager( num, *args, **kwargs ):
     """
@@ -95,6 +100,8 @@ class FigureCanvasQTAgg( FigureCanvasQT, FigureCanvasAgg ):
             else:
                 stringBuffer = self.renderer._renderer.tostring_argb()
 
+            refcnt = sys.getrefcount(stringBuffer)
+
             qImage = QtGui.QImage(stringBuffer, self.renderer.width,
                                   self.renderer.height,
                                   QtGui.QImage.Format_ARGB32)
@@ -106,6 +113,11 @@ class FigureCanvasQTAgg( FigureCanvasQT, FigureCanvasAgg ):
                 p.setPen( QtGui.QPen( QtCore.Qt.black, 1, QtCore.Qt.DotLine ) )
                 p.drawRect( self.rect[0], self.rect[1], self.rect[2], 
self.rect[3] )
             p.end()
+
+            del qImage
+            if refcnt != sys.getrefcount(stringBuffer):
+                # Fix a memory leak in PySide on Python 3
+                decref(stringBuffer)
         else:
             bbox = self.blitbox
             l, b, r, t = bbox.extents
------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to