jupyter-widgets/pythreejs

[pythreejs.BufferGeometry] Memory continues to grow

spencergotowork opened this issue · 6 comments

Does the memory of this part of BufferGeometry automatically reclaim? Or do I need a manual recycling method? I called this in a function, but left the function to show that memory kept growing. like that:

截图 2022-09-02 14-37-13

import gc
for i in range(10000):
    g3 = LineSegmentsGeometry(
        positions=[
            [[0, 0, 0], [1, 1, 1]],
            [[2, 2, 2], [4, 4, 4]]
        ],
        colors=[
            [[1, 0, 0], [1, 0, 0]],
            [[0, 1, 0], [0, 0, 1]]
        ],
    )
    m3 = LineMaterial(linewidth=10, vertexColors='VertexColors')
    line3 = LineSegments2(g3, m3)

    del g3, m3, line3
    
    if (i%100) == 0:
        gc.collect()

Take this code, memory will keep going up. Is there a way to clear the memory of BufferAttribute or LineSegmentsGeometry.
I use scene.add and scene.remove to animate the demo, each frame has a different number of objects, so I can't simply modify geometry. attributes to get the result

Were you running this in Jupyter? Could this have to do with the fact that ipython by default holds on to references of the cell outputs, and thus the memory does not get freed? (see https://ipython.readthedocs.io/en/stable/interactive/reference.html?highlight=previous#output-caching-system)

Although your code doesn't seem to be producing any output...

Were you running this in Jupyter? Could this have to do with the fact that ipython by default holds on to references of the cell outputs, and thus the memory does not get freed? (see https://ipython.readthedocs.io/en/stable/interactive/reference.html?highlight=previous#output-caching-system)

Although your code doesn't seem to be producing any output...

Thank you for your reply. I have been running on jupyter lab and can see that memory is growing gradually.

Can you try running the same code in a python script to see if the memory also grows there?

This is probably related to this general ipywidgets issue: jupyter-widgets/ipywidgets#1345

i.e. you probably need to call .close() on the widget before calling del to ensure it gets cleaned up properly. As @nvaytet points out, any widget included in an output can also possibly be kept a reference to via ipython output caching. But at least for your simplest example above, .close() should be the needed piece.