/external/matplotlib/unit/memleak_gui.py

https://bitbucket.org/jmcmorris/escape · Python · 109 lines · 80 code · 11 blank · 18 comment · 12 complexity · 9808ad23155270e291917c94456b2269 MD5 · raw file

  1. #!/usr/bin/env python
  2. '''
  3. This illustrates a leak that occurs with any interactive backend.
  4. Run with :
  5. > python memleak_gui.py -dGTKAgg # or TkAgg, etc..
  6. use --help option to see all options
  7. The default number of loops typically will not yield a stable
  8. estimate--for that you may need many hundreds of loops and some patience.
  9. You may need to edit cbook.report_memory to support your platform
  10. '''
  11. from __future__ import print_function
  12. import os, sys, time
  13. import gc
  14. from optparse import OptionParser
  15. parser = OptionParser()
  16. parser.add_option("-q", "--quiet", default=True,
  17. action="store_false", dest="verbose")
  18. parser.add_option("-s", "--start", dest="start",
  19. default="30",
  20. help="first index of averaging interval")
  21. parser.add_option("-e", "--end", dest="end",
  22. default="100",
  23. help="last index of averaging interval")
  24. parser.add_option("-t", "--toolbar", dest="toolbar",
  25. default="toolbar2",
  26. help="toolbar: None, classic, toolbar2")
  27. # The following overrides matplotlib's version of the -d option
  28. # uses it if found
  29. parser.add_option("-d", "--backend", dest="backend",
  30. default='',
  31. help="backend")
  32. parser.add_option("-c", "--cycles", dest="cycles",
  33. default=False, action="store_true")
  34. options, args = parser.parse_args()
  35. indStart = int(options.start)
  36. indEnd = int(options.end)
  37. import matplotlib
  38. matplotlib.rcParams['toolbar'] = matplotlib.validate_toolbar(options.toolbar)
  39. if options.backend:
  40. matplotlib.use(options.backend)
  41. import pylab
  42. import matplotlib.cbook as cbook
  43. print('# columns are: iteration, OS memory (k), number of python objects')
  44. print('#')
  45. for i in range(indEnd+1):
  46. fig = pylab.figure()
  47. fig.savefig('test') # This seems to just slow down the testing.
  48. fig.clf()
  49. pylab.close(fig)
  50. gc.collect()
  51. val = cbook.report_memory(i)
  52. if options.verbose:
  53. if i % 10 == 0:
  54. #print ("iter: %4d OS memory: %8d Python objects: %8d" %
  55. print ("%4d %8d %8d" %
  56. (i, val, len(gc.get_objects())))
  57. if i==indStart: start = val # wait a few cycles for memory usage to stabilize
  58. gc.collect()
  59. end = val
  60. print('# columns above are: iteration, OS memory (k), number of python objects')
  61. print('#')
  62. print('# uncollectable list:', gc.garbage)
  63. print('#')
  64. if i > indStart:
  65. print('# Backend %(backend)s, toolbar %(toolbar)s' % matplotlib.rcParams)
  66. backend = options.backend.lower()
  67. if backend.startswith("gtk"):
  68. import gtk
  69. import gobject
  70. print("# pygtk version: %s, gtk version: %s, pygobject version: %s, glib version: %s" % \
  71. (gtk.pygtk_version, gtk.gtk_version,
  72. gobject.pygobject_version, gobject.glib_version))
  73. elif backend.startswith("qt4"):
  74. import PyQt4.pyqtconfig
  75. print("# PyQt4 version: %s, Qt version %x" % \
  76. (PyQt4.pyqtconfig.Configuration().pyqt_version_str,
  77. PyQt4.pyqtconfig.Configuration().qt_version))
  78. elif backend.startswith("qt"):
  79. import pyqtconfig
  80. print("# pyqt version: %s, qt version: %x" % \
  81. (pyqtconfig.Configuration().pyqt_version_str,
  82. pyqtconfig.Configuration().qt_version))
  83. elif backend.startswith("wx"):
  84. import wx
  85. print("# wxPython version: %s" % wx.__version__)
  86. elif backend.startswith("tk"):
  87. import Tkinter
  88. print("# Tkinter version: %s, Tk version: %s, Tcl version: %s" % (Tkinter.__version__, Tkinter.TkVersion, Tkinter.TclVersion))
  89. print('# Averaging over loops %d to %d' % (indStart, indEnd))
  90. print('# Memory went from %dk to %dk' % (start, end))
  91. print('# Average memory consumed per loop: %1.4fk bytes\n' % ((end-start)/float(indEnd-indStart)))
  92. if options.cycles:
  93. cbook.print_cycles(gc.garbage)