Skip to content

Memory Leak in Agg #3197

Closed
Closed

Description

@letmaik

First, the relevant output of valgrind with --tool=memcheck --leak-check=full --show-leak-kinds=all:

==2657== 4,841,396 bytes in 2 blocks are still reachable in loss record 4,337 of 4,341
==2657==    at 0x4C2BC79: operator new[](unsigned long) (vg_replace_malloc.c:384)
==2657==    by 0x2D592353: RendererAgg::tostring_rgba_minimized(Py::Tuple const&) (_backend_agg.cpp:2382)
==2657==    by 0x2D5A3363: Py::PythonExtension<RendererAgg>::method_varargs_call_handler(_object*, _object*) (ExtensionOldType.hxx:291)
==2657==    by 0x54BB13: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x577BE1: ??? (in /usr/bin/python2.7)
==2657== 
==2657== 55,424,972 bytes in 33 blocks are definitely lost in loss record 4,339 of 4,341
==2657==    at 0x4C2BC79: operator new[](unsigned long) (vg_replace_malloc.c:384)
==2657==    by 0x2D592353: RendererAgg::tostring_rgba_minimized(Py::Tuple const&) (_backend_agg.cpp:2382)
==2657==    by 0x2D5A3363: Py::PythonExtension<RendererAgg>::method_varargs_call_handler(_object*, _object*) (ExtensionOldType.hxx:291)
==2657==    by 0x54BB13: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x577BE1: ??? (in /usr/bin/python2.7)
==2657== 
==2657== 56,230,256 bytes in 25 blocks are possibly lost in loss record 4,340 of 4,341
==2657==    at 0x4C2BC79: operator new[](unsigned long) (vg_replace_malloc.c:384)
==2657==    by 0x2D592353: RendererAgg::tostring_rgba_minimized(Py::Tuple const&) (_backend_agg.cpp:2382)
==2657==    by 0x2D5A3363: Py::PythonExtension<RendererAgg>::method_varargs_call_handler(_object*, _object*) (ExtensionOldType.hxx:291)
==2657==    by 0x54BB13: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x54C027: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==2657==    by 0x575D91: PyEval_EvalCodeEx (in /usr/bin/python2.7)
==2657==    by 0x577BE1: ??? (in /usr/bin/python2.7)
==2657== 
==2657== LEAK SUMMARY:
==2657==    definitely lost: 55,490,524 bytes in 83 blocks
==2657==    indirectly lost: 20,256 bytes in 407 blocks
==2657==      possibly lost: 56,383,520 bytes in 481 blocks
==2657==    still reachable: 11,403,937 bytes in 13,860 blocks
==2657==         suppressed: 179,227,068 bytes in 27,533 blocks

I am drawing many maps (with basemap) with polygon collections using the agg backend and then save them as png or svg. I always close the figures and start fresh. The more I draw the higher the memory usage gets, which is why I ran memcheck. I will try to come up with a minimal example that reproduces this.

I am using mpl 1.3.1, basemap 1.0.7, Python 2.7.3.

Activity

  1. tacaswell commented on Jul 8, 2014

    @tacaswell
    Member

    you provide simple code to reproduce this? Does this also happen on master?

  2. letmaik commented on Jul 8, 2014

    @letmaik
    ContributorAuthor

    Right, I narrowed down the problem quite a lot. It's a combination of using agg, rasterized, and saving as svg:

    import numpy as np
    import matplotlib as mpl
    mpl.use('agg')
    import matplotlib.pyplot as plt
    
    for i in range(100000):
        fig, ax = plt.subplots()         
    
        circle1=plt.Circle((0,0),2,color='r',rasterized=True)
        ax.add_artist(circle1)
    
        fig.savefig('test.svg')
        plt.close(fig)

    There's no memory leak when one of the following is true: using another backend (e.g. cairo), not using rasterized, not using svg as output.

    EDIT: removed unused code parts

  3. tacaswell commented on Jul 8, 2014

    @tacaswell
    Member

    Looking at the source for tostring_rgba_minimize it looks like it allocates a buffer which it does not de-allocate, however the pointer gets handed off to either Py:Bytes (py3k) or Py:String (py2k) which the comments suggest should take over management of that pointer.

  4. letmaik commented on Jul 8, 2014

    @letmaik
    ContributorAuthor

    I don't know about the internal structure of matplotlib, but I suspect that tostring_rgba_minimize is called to rasterize the circle (=buf), then it is included in the svg somewhere else, but the raster buffer is not freed there.

  5. tacaswell commented on Jul 9, 2014

    @tacaswell
    Member

    My point was that the code that allocates this buffer is not responsible for cleaning it up and that the valgrind might be a red herring.

  6. letmaik commented on Jul 9, 2014

    @letmaik
    ContributorAuthor

    I think you're right, after all valgrind doesn't tell you where the leak is. Just to confirm, you can reproduce the memory increase using the test script, right?

  7. tacaswell commented on Jul 9, 2014

    @tacaswell
    Member

    I have not had time to really dig into this. I think @efiring has done a good deal of work on memory leaks.

  8. dopplershift commented on Jul 11, 2014

    @dopplershift
    Contributor

    We're using the PyCXX API (and hence Python C-API) incorrectly. The functions we use make copies of the buffer and do not assume ownership. I'm working on a patch to have Python allocate the memory and give us the pointer, so that we no long leak and avoid the copy.

  9. added a commit that references this issue on Jul 11, 2014
  10. tacaswell commented on Jul 12, 2014

    @tacaswell
    Member

    @neothemachine This should be fixed, closing. Please ping us if that isn't true.

  11. letmaik commented on Jul 14, 2014

    @letmaik
    ContributorAuthor

    Will this be in 1.3.x as well? That would be good, as I just realized that rasterized is not supported by the cairo backend, so there's no workaround for me at the moment if I want to keep using svg's.

  12. tacaswell commented on Jul 14, 2014

    @tacaswell
    Member

    At this point we have no plans for 1.3.2

    We just tagged the first rc for 1.4.0
    On Jul 14, 2014 7:04 AM, "Maik Riechert" notifications@github.com wrote:

    Will this be in 1.3.x as well? That would be good, as I just realized that
    rasterized is not supported by the cairo backend, so there's no
    workaround for me at the moment if I want to keep using svg's.

    —
    Reply to this email directly or view it on GitHub
    #3197 (comment)
    .

  13. letmaik commented on Jul 14, 2014

    @letmaik
    ContributorAuthor

    Ok, I will wait for 1.4.0 then. In the meantime I will install the rc and check the fix.

  14. tacaswell commented on Jul 14, 2014

    @tacaswell
    Member

    Yes, please test the rc

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions