Skip to content

Commit 438015d

Browse files
committed
added a modified version of erics memleak script for guis
svn path=/trunk/matplotlib/; revision=3132
1 parent 9d9c5b5 commit 438015d

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ class NavigationToolbar2:
11511151

11521152
def __init__(self, canvas):
11531153
self.canvas = canvas
1154-
1154+
canvas.toolbar = self
11551155
# a dict from axes index to a list of view limits
11561156
self._views = Stack()
11571157
self._positions = Stack() # stack of subplot positions

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ def destroy(self, *args):
311311
if self.window is not None:
312312
self.window.quit()
313313
if self.window is not None:
314-
#print 'calling window destroy'
314+
#self.toolbar.destroy()
315315
self.window.destroy()
316+
316317
pass
317318
self.window = None
318319

@@ -534,7 +535,7 @@ def __init__(self, canvas, window):
534535
self.canvas = canvas
535536
self.window = window
536537
self._idle = True
537-
Tk.Frame.__init__(self, master=self.canvas._tkcanvas)
538+
#Tk.Frame.__init__(self, master=self.canvas._tkcanvas)
538539
NavigationToolbar2.__init__(self, canvas)
539540

540541
def set_message(self, s):

lib/matplotlib/cbook.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,19 @@ def reverse_dict(d):
685685
return dict([(v,k) for k,v in d.items()])
686686

687687

688+
def report_memory(i):
689+
'return the memory consumed by process'
690+
pid = os.getpid()
691+
if sys.platform=='sunos5':
692+
a2 = os.popen('ps -p %d -o osz' % pid).readlines()
693+
mem = int(a2[-1].strip())
694+
#print i, ' ', int(a2[-1].strip())
695+
elif sys.platform.startswith('linux'):
696+
a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
697+
mem = int(a2[1].split()[1])
698+
699+
return mem
700+
688701
if __name__=='__main__':
689702
assert( allequal([1,1,1]) )
690703
assert(not allequal([1,1,0]) )

lib/matplotlib/figure.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def __init__(self,
153153
self.subplotpars = subplotpars
154154

155155
self._axstack = Stack() # maintain the current axes
156+
self.axes = []
156157
self.clf()
157158

158159
self._cachedRenderer = None
@@ -514,7 +515,13 @@ def clf(self):
514515
"""
515516
Clear the figure
516517
"""
517-
self.axes = []
518+
for ax in self.axes:
519+
ax.cla()
520+
self.delaxes(ax)
521+
522+
toolbar = getattr(self.canvas, 'toolbar', None)
523+
if toolbar is not None:
524+
toolbar.update()
518525
self._axstack.clear()
519526
self._seen = {}
520527
self.lines = []

unit/memleak_gui.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
'''
3+
This illustrates a leak that occurs with any interactive backend.
4+
'''
5+
import os, sys, time
6+
import gc
7+
import matplotlib
8+
9+
#matplotlib.use('TkAgg') # or TkAgg or WxAgg or QtAgg or Gtk
10+
matplotlib.rcParams['toolbar'] = 'toolbar2' # None, classic, toolbar2
11+
#matplotlib.rcParams['toolbar'] = None # None, classic, toolbar2
12+
13+
import pylab
14+
from matplotlib import _pylab_helpers as ph
15+
import matplotlib.cbook as cbook
16+
17+
indStart, indEnd = 30, 50
18+
for i in range(indEnd):
19+
20+
fig = pylab.figure()
21+
fig.savefig('test')
22+
fig.clf()
23+
pylab.close(fig)
24+
val = cbook.report_memory(i)
25+
print i, val
26+
gc.collect()
27+
if i==indStart: start = val # wait a few cycles for memory usage to stabilize
28+
29+
gc.collect()
30+
print
31+
print 'uncollectable list:', gc.garbage
32+
print
33+
end = val
34+
if i > indStart:
35+
print 'Average memory consumed per loop: %1.4fk bytes\n' % ((end-start)/float(indEnd-indStart))
36+

0 commit comments

Comments
 (0)