DougHamil / threeagent

ClojureScript library for building Three.js apps in a reagent-like fashion
MIT License
134 stars 10 forks source link

Garbage Leak? #32

Closed joinr closed 2 years ago

joinr commented 2 years ago

I am working with a scene that's allocating and deallocating sprites quite a bit, although I have cached the materials use for the sprites. So I "think" the virtual scene is adding and removing nodes quite often. Over time, I am getting a pretty big memory leak (on the order of gigs, building from < 100 mb), despite having what should be more-or-less constant average memory usage. It looks like the implementation in virtual scene is not calling dispose anywhere. Reading three.js docs, it seems like this may be necessary (particularly in a reactive context where the scene graph is changing). Any ideas? I am looking at manually patching in some calls to dispose to see if it fixes my problem.

joinr commented 2 years ago

For additional context, I am producing a ton of ArrayBuffer instances (seems like shared memory with gpu maybe, or workers).

joinr commented 2 years ago

I am also calling into an emscripten compiled lib, yoga-layout, for doing flexbox style layouts. I am calling the API's free methods on the layout objects (these are are being invoked quite a bit too) and have not nailed down if this is generating garbage; could be unreleated to three/threeagent still.

joinr commented 2 years ago

looks like a design issue with three with a patch pending https://github.com/mrdoob/three.js/issues/5821 damn. I don't recall seeing any component definition for sprites in the base lib or examples (I made my own defcomponent on top of three's sprite object). Looks like either work around or move away from sprites, since the textures appear to be re-uploaded to webgl every time (outside of involved work arounds).

joinr commented 2 years ago

wow, so looks like it had nothing to do with sprites. I turned off some :text elements I had generated with:

[:object {:position [-3 6 0]}
        [:text {:text "Blah"
                :material (js/THREE.MeshPhongMaterial. #js{:color "black"})
                :font font
                :height 0.1
                :size 0.5}]]

And memory use went down from like 3gb eventually crashing to 40mb constant, with sprites involved.

joinr commented 2 years ago

Caching the material solved everything. I am guessing it was triggering new object creation and upload every frame or something. Much to learn. Maybe someone else learns from my mistakes :)