/unit/memleak_gui.py

https://gitlab.com/sagarjhaa/matplotlib · Python · 103 lines · 74 code · 11 blank · 18 comment · 11 complexity · 28f7ad17dd6bbe3153e17ebde6bfe2c6 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 gc
  13. from optparse import OptionParser
  14. parser = OptionParser()
  15. parser.add_option("-q", "--quiet", default=True,
  16. action="store_false", dest="verbose")
  17. parser.add_option("-s", "--start", dest="start",
  18. default="30",
  19. help="first index of averaging interval")
  20. parser.add_option("-e", "--end", dest="end",
  21. default="100",
  22. help="last index of averaging interval")
  23. parser.add_option("-t", "--toolbar", dest="toolbar",
  24. default="toolbar2",
  25. help="toolbar: None, classic, toolbar2")
  26. # The following overrides matplotlib's version of the -d option
  27. # uses it if found
  28. parser.add_option("-d", "--backend", dest="backend",
  29. default='',
  30. help="backend")
  31. parser.add_option("-c", "--cycles", dest="cycles",
  32. default=False, action="store_true")
  33. options, args = parser.parse_args()
  34. indStart = int(options.start)
  35. indEnd = int(options.end)
  36. import matplotlib
  37. matplotlib.rcParams['toolbar'] = matplotlib.validate_toolbar(options.toolbar)
  38. if options.backend:
  39. matplotlib.use(options.backend)
  40. import pylab
  41. import matplotlib.cbook as cbook
  42. print('# columns are: iteration, OS memory (k), number of python objects')
  43. print('#')
  44. for i in range(indEnd + 1):
  45. fig = pylab.figure()
  46. fig.savefig('test') # This seems to just slow down the testing.
  47. fig.clf()
  48. pylab.close(fig)
  49. gc.collect()
  50. val = cbook.report_memory(i)
  51. if options.verbose:
  52. if i % 10 == 0:
  53. #print ("iter: %4d OS memory: %8d Python objects: %8d" %
  54. print("%4d %8d %8d" % (i, val, len(gc.get_objects())))
  55. if i == indStart:
  56. start = val # wait a few cycles for memory usage to stabilize
  57. gc.collect()
  58. end = val
  59. print('# columns above are: iteration, OS memory (k), number of python objects')
  60. print('#')
  61. print('# uncollectable list:', gc.garbage)
  62. print('#')
  63. if i > indStart:
  64. print('# Backend %(backend)s, toolbar %(toolbar)s' % matplotlib.rcParams)
  65. backend = options.backend.lower()
  66. if backend.startswith("gtk"):
  67. import gtk
  68. import gobject
  69. print("# pygtk version: %s, gtk version: %s, pygobject version: %s, glib version: %s" % \
  70. (gtk.pygtk_version, gtk.gtk_version,
  71. gobject.pygobject_version, gobject.glib_version))
  72. elif backend.startswith("qt4"):
  73. import PyQt4.pyqtconfig
  74. print("# PyQt4 version: %s, Qt version %x" % \
  75. (PyQt4.pyqtconfig.Configuration().pyqt_version_str,
  76. PyQt4.pyqtconfig.Configuration().qt_version))
  77. elif backend.startswith("wx"):
  78. import wx
  79. print("# wxPython version: %s" % wx.__version__)
  80. elif backend.startswith("tk"):
  81. import Tkinter
  82. print("# Tkinter version: %s, Tk version: %s, Tcl version: %s" % (Tkinter.__version__, Tkinter.TkVersion, Tkinter.TclVersion))
  83. print('# Averaging over loops %d to %d' % (indStart, indEnd))
  84. print('# Memory went from %dk to %dk' % (start, end))
  85. print('# Average memory consumed per loop: %1.4fk bytes\n' % ((end - start) / float(indEnd - indStart)))
  86. if options.cycles:
  87. cbook.print_cycles(gc.garbage)