Skip to content

apparent memory leak with live plotting #11956

Closed

Description

@mgeorgiadis

Bug report

I've noticed that when running a matplotlib as a run-chart/long term plotting tool, memory use by matplotlib increases over the course of about a week, ultimately crashing the program. I'm continually updating my plot, but I am only plotting the most recent 100 data points (so that the data itself isn't contributing to the memory increase). See an overly simplified example below that still shows the issue. As you will see when running the code below, matplotlib\transforms.py just continually increases by around 5-15 kb on each turn of the loop, despite clearing my axes and using the object oriented version of matplotlib. This increase will continue to happen until it fills up the memory of my computer. Is this intentional? My thought was that in the object oriented program version of matplotlib, my code shouldn't increase in memory like this.

Most of the code below is simply just tracemalloc helper functions for keeping track of memory.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np
import os
import linecache
import sys
import tracemalloc
import time


def display_top(snapshot, key_type='lineno', limit=2):
    '''
    function for pretty printing tracemalloc output
    '''
    snapshot = snapshot.filter_traces((
        tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
        tracemalloc.Filter(False, "<unknown>"),
    ))
    top_stats = snapshot.statistics(key_type)

    print("Top %s lines" % limit)
    for index, stat in enumerate(top_stats[:limit], 1):
        frame = stat.traceback[0]
        # replace "/path/to/module/file.py" with "module/file.py"
        filename = os.sep.join(frame.filename.split(os.sep)[-2:])
        print("#%s: %s:%s: %.1f KiB"
              % (index, filename, frame.lineno, stat.size / 1024))
        line = linecache.getline(frame.filename, frame.lineno).strip()
        if line:
            print('    %s' % line)

    other = top_stats[limit:]
    if other:
        size = sum(stat.size for stat in other)
        print("%s other: %.1f KiB" % (len(other), size / 1024))
    total = sum(stat.size for stat in top_stats)
    print("Total allocated size: %.1f KiB" % (total / 1024))

tracemalloc.start()

y = np.random.rand(100)
x = range(len(y))


fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'b-')
plt.show(block=False)

t0 = time.time()
 
while True:
    try:
        ax.clear()
        ax.plot(x,np.random.rand(100),'b-')
        plt.pause(0.0001)
        snapshot = tracemalloc.take_snapshot()
        print("Run Time:", time.time()-t0)
        display_top(snapshot)
        time.sleep(0.05)
    except KeyboardInterrupt:
        break

If you'd prefer to use your own memory debugger, here's the simplest form of this issue:

import matplotlib.pyplot as plt
import numpy as np
import time
y = np.random.rand(100)
x = range(len(y))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'b-')
plt.show(block=False)

t0 = time.time()
 
while True:
    try:
        ax.clear()
        ax.plot(x,np.random.rand(100),'b-')
        plt.pause(0.0001)
        print("Run Time:", time.time()-t0)
        time.sleep(0.05)
    except KeyboardInterrupt:
        break

In the console output, you'll see matplotlib\transforms.py quickly become the largest memory holder, and increase continually.

This issue also appears if I use a line object and line.set_xdata, etc., rather than ax.plot().

Using WinPython, Python 3.6.2
Matplotlib version 2.2.2
Windows 7 and Windows 10
Backend TkAgg

Activity

  1. jklymak commented on Aug 28, 2018

    @jklymak
    Member

    I can replicate this on master, at least using your test, and a line.set_ydata. You have to wait a while for the transform.py code to dominate the memory, but it does after a while at about 3kb/iteration.

    #2: matplotlib/transforms.py:180: 613.0 KiB
        child._parents[id(self)] = weakref.ref(self)
    
  2. jklymak commented on Aug 28, 2018

    @jklymak
    Member

    child._parents keeps growing without bound here. Not quite clear to me how the child parent relationship works here, or why the same child keeps accumulating new parents.

  3. WeatherGod commented on Aug 28, 2018

    @WeatherGod
    Member
  4. added this to the v3.1 milestone on Aug 28, 2018
  5. tacaswell commented on Aug 28, 2018

    @tacaswell
    Member

    Does throwing in explicit gc (ex gc.collect(2)) help?

    Is the issue that clear is not getting enough things?

    How are we generating new transform objects when using set_ydata? Can you reproduce this just by drawing repeatedly?

  6. mgeorgiadis commented on Aug 28, 2018

    @mgeorgiadis
    Author

    Throwing in explicit gc did not appear to help the problem.

    Based on my reading of transforms, it appears that the code creates some sort of additional "weak" reference between parents and children. Is this weak reference what is causing the size of the child._parents to grow continually? I don't know much about the inner workings of matplotlib or what this weakref module is.

    Snippet from transforms.py

    # Parents are stored as weak references, so that if the
    # parents are destroyed, references from the children won't
    # keep them alive.
    for child in children:
        child._parents[id(self)] = weakref.ref(self)
    
  7. tacaswell commented on Aug 28, 2018

    @tacaswell
    Member

    Ah, so it is the weakref objects them selves which are leaking? Maybe we just need to every so often clean out child._parents?

    weakref is a built in python module (see https://docs.python.org/3/library/weakref.html).

  8. jklymak commented on Aug 28, 2018

    @jklymak
    Member

    Child._parents keeps growing. Not really sure why other than each draw of line object does make a new transform.

  9. anntzer commented on Aug 28, 2018

    @anntzer
    Contributor

    Or just use a WeakSet? Something like

    diff --git i/lib/matplotlib/transforms.py w/lib/matplotlib/transforms.py
    index 7ba551091..9af25ebf0 100644
    --- i/lib/matplotlib/transforms.py
    +++ w/lib/matplotlib/transforms.py
    @@ -36,6 +36,7 @@ themselves.
     import re
     import warnings
     import weakref
    +from weakref import WeakSet
     
     import numpy as np
     from numpy.linalg import inv
    @@ -91,7 +92,7 @@ class TransformNode(object):
                 no significance other than to improve the readability of
                 ``str(transform)`` when DEBUG=True.
             """
    -        self._parents = {}
    +        self._parents = WeakSet()
     
             # TransformNodes start out as invalid until their values are
             # computed for the first time.
    @@ -104,15 +105,13 @@ class TransformNode(object):
                 return self._shorthand_name or repr(self)
     
         def __getstate__(self):
    -        # turn the dictionary with weak values into a normal dictionary
    -        return {**self.__dict__,
    -                '_parents': {k: v() for k, v in self._parents.items()}}
    +        # Convert the weakset to a normal one.
    +        return {**self.__dict__, '_parents': set(self._parents)}
     
         def __setstate__(self, data_dict):
             self.__dict__ = data_dict
    -        # turn the normal dictionary back into a dictionary with weak values
    -        self._parents = {k: weakref.ref(v)
    -                         for k, v in self._parents.items() if v is not None}
    +        # Turn the normal set back into a weak one.
    +        self._parents = WeakSet(self._parents)
     
         def __copy__(self, *args):
             raise NotImplementedError(
    @@ -151,12 +150,10 @@ class TransformNode(object):
             if self.pass_through or status_changed:
                 self._invalid = value
     
    -            for parent in list(self._parents.values()):
    -                # Dereference the weak reference
    -                parent = parent()
    -                if parent is not None:
    -                    parent._invalidate_internal(
    -                        value=value, invalidating_node=self)
    +            # Convert the WeakSet to a strong container before iterating on it.
    +            for parent in list(self._parents):
    +                parent._invalidate_internal(
    +                    value=value, invalidating_node=self)
     
         def set_children(self, *children):
             """
    @@ -169,7 +166,7 @@ class TransformNode(object):
             # parents are destroyed, references from the children won't
             # keep them alive.
             for child in children:
    -            child._parents[id(self)] = weakref.ref(self)
    +            child._parents.add(self)
     
         if DEBUG:
             _set_children = set_children
    

    can someone see whether this fixes it?

  10. jklymak commented on Aug 28, 2018

    @jklymak
    Member

    Away from computer for a while but happy to try. Imagine this’ll fix the issue but a little confused why this happens in the first place.

  11. jklymak commented on Aug 29, 2018

    @jklymak
    Member

    @anntzer

    Traceback (most recent call last):
      File "testmemleak.py", line 44, in <module>
        fig = plt.figure()
      File "/Users/jklymak/matplotlib/lib/matplotlib/pyplot.py", line 563, in figure
        **kwargs)
      File "/Users/jklymak/matplotlib/lib/matplotlib/backend_bases.py", line 3217, in new_figure_manager
        fig = fig_cls(*args, **kwargs)
      File "/Users/jklymak/matplotlib/lib/matplotlib/figure.py", line 365, in __init__
        self.transFigure = BboxTransformTo(self.bbox)
      File "/Users/jklymak/matplotlib/lib/matplotlib/transforms.py", line 2626, in __init__
        self.set_children(boxout)
      File "/Users/jklymak/matplotlib/lib/matplotlib/transforms.py", line 169, in set_children
        child._parents.add(self)
      File "/Users/jklymak/anaconda3/envs/matplotlibdev/lib/python3.6/_weakrefset.py", line 84, in add
        self.data.add(ref(item, self._remove))
    TypeError: unhashable type: 'BboxTransformTo'
    
  12. jklymak commented on Aug 29, 2018

    @jklymak
    Member

    WeakValueDictionary seems to work, though I'm not sure if I understand the __get/settstate__ stuff well enough to know if that is still working...

  13. anntzer commented on Aug 29, 2018

    @anntzer
    Contributor

    Seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions