jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
934 stars 185 forks source link

[pythreejs.BufferGeometry] Memory continues to grow #387

Open spencergotowork opened 1 year ago

spencergotowork commented 1 year ago

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

spencergotowork commented 1 year ago
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

nvaytet commented 1 year ago

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...

spencergotowork commented 1 year ago

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.

nvaytet commented 1 year ago

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

vidartf commented 1 year ago

This is probably related to this general ipywidgets issue: https://github.com/jupyter-widgets/ipywidgets/issues/1345

vidartf commented 1 year ago

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.