File: memleak_gui.py

package info (click to toggle)
matplotlib 2.0.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 91,640 kB
  • ctags: 29,525
  • sloc: python: 122,697; cpp: 60,806; ansic: 30,799; objc: 2,830; makefile: 224; sh: 85
file content (103 lines) | stat: -rwxr-xr-x 3,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python

'''
This illustrates a leak that occurs with any interactive backend.
Run with :

  > python memleak_gui.py -dGTKAgg   # or TkAgg, etc..

use --help option to see all options

The default number of loops typically will not yield a stable
estimate--for that you may need many hundreds of loops and some patience.

You may need to edit cbook.report_memory to support your platform

'''
from __future__ import print_function
import gc
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-q", "--quiet", default=True,
                  action="store_false", dest="verbose")
parser.add_option("-s", "--start", dest="start",
                  default="30",
                  help="first index of averaging interval")
parser.add_option("-e", "--end", dest="end",
                  default="100",
                  help="last index of averaging interval")
parser.add_option("-t", "--toolbar", dest="toolbar",
                  default="toolbar2",
                  help="toolbar: None, classic, toolbar2")
# The following overrides matplotlib's version of the -d option
# uses it if found
parser.add_option("-d", "--backend", dest="backend",
                  default='',
                  help="backend")
parser.add_option("-c", "--cycles", dest="cycles",
                  default=False, action="store_true")

options, args = parser.parse_args()

indStart = int(options.start)
indEnd = int(options.end)
import matplotlib
matplotlib.rcParams['toolbar'] = matplotlib.validate_toolbar(options.toolbar)
if options.backend:
    matplotlib.use(options.backend)
import pylab
import matplotlib.cbook as cbook

print('# columns are: iteration, OS memory (k), number of python objects')
print('#')
for i in range(indEnd + 1):

    fig = pylab.figure()
    fig.savefig('test')  # This seems to just slow down the testing.
    fig.clf()
    pylab.close(fig)
    gc.collect()
    val = cbook.report_memory(i)
    if options.verbose:
        if i % 10 == 0:
            #print ("iter: %4d OS memory: %8d Python objects: %8d" %
            print("%4d %8d %8d" % (i, val, len(gc.get_objects())))
    if i == indStart:
        start = val  # wait a few cycles for memory usage to stabilize

gc.collect()
end = val

print('# columns above are: iteration, OS memory (k), number of python objects')
print('#')
print('# uncollectable list:', gc.garbage)
print('#')

if i > indStart:
    print('# Backend %(backend)s, toolbar %(toolbar)s' % matplotlib.rcParams)
    backend = options.backend.lower()
    if backend.startswith("gtk"):
        import gtk
        import gobject
        print("# pygtk version: %s, gtk version: %s, pygobject version: %s, glib version: %s" % \
            (gtk.pygtk_version, gtk.gtk_version,
             gobject.pygobject_version, gobject.glib_version))
    elif backend.startswith("qt4"):
        import PyQt4.pyqtconfig
        print("# PyQt4 version: %s, Qt version %x" % \
            (PyQt4.pyqtconfig.Configuration().pyqt_version_str,
             PyQt4.pyqtconfig.Configuration().qt_version))
    elif backend.startswith("wx"):
        import wx
        print("# wxPython version: %s" % wx.__version__)
    elif backend.startswith("tk"):
        import Tkinter
        print("# Tkinter version: %s, Tk version: %s, Tcl version: %s" % (Tkinter.__version__, Tkinter.TkVersion, Tkinter.TclVersion))

    print('# Averaging over loops %d to %d' % (indStart, indEnd))
    print('# Memory went from %dk to %dk' % (start, end))
    print('# Average memory consumed per loop: %1.4fk bytes\n' % ((end - start) / float(indEnd - indStart)))

if options.cycles:
    cbook.print_cycles(gc.garbage)