Creating and disposing whole scenes

Hi, i am struggeling with a memory leak in my patch and wonder why and how to avoid that. I have patched a basic example to demonstrate the issue hoping for any advice to handle that task.

I want to create whole scenes in a class. They are short lived and need to be disposed when done. There will be bit more going on in the class like animation, writing textures to disk and some more that is not part of this example which already leaks memory in its basic form.

The class that is instantiated implements the IDisposible interface and i am calling dispose when an instance is removed. The dispose apperas to work in general but it seems it does not get everything. The memory allocation of vvvv is growing fairly fast. I have no clue how to debug this further:(

leak.vl (39.7 KB)

Even with a more basic patch, no cloning, no materials memory is growing.

What i also notice even with the super basic patch is GPU Memory growing super fast, can be also seen in task manager

When GPU memory is at limit, everything seems get into RAM and the leak speeds up a lot (be careful, when RAM is full it will also imapact whole windows, in my case the gpu driver is crashing with some other windows stuff)

2 Likes

Hey tgd,

I had a look, and the culprit seems to be the SceneTexture.
leak_workaround.vl (55.9 KB)

When you remove it from your class, the texture memory doesn’t increase.

I know it’s probably not a solution for you, as each scene might have different PostFX, etc. Just wanted to let you know that I found some way to get it not to leak. Also you would need to change the contract between System and Scenes. So that’s not ideal.

We’ll look at what we can do regarding SceneTexture leaking memory. It’s a quite a big one as it comes with its own PostFX and Compositor.

That said, I was wondering if your system would maybe be even more flexible if you tried to separate scene and scene rendering. You might be able to come up with a system where you can choose Scene and PostFX independently from one another. Maybe the Scene outputs a hint about which PostFX would be preferred.
If this would be an option for you, you could patch your own SceneTexture out of RenderTexture and a PostFX that doesn’t get recreated all the time.

Had a look at some sort of refactoring where you don’t recreate the Compositor Setup all the time.
leak_workaround2.vl (66.6 KB)

3 Likes

@gregsn as always, a lovely clear example, thanks!

1 Like

sorry for coming back that late but i needed to take a long break from this topic.
first thanks for looking into it and the proposed workaround.
unfortunately it does not work well for my case. as i mentioned in the initial post, i need to do some other things while rendering the scene, one of them beeing texture writing.
(i am using incremental mainloop so the writer can block the mainloop for each write)

now with the scene rendering in a seperate loop i need to do the writing in this seperate loop. this does not go well with changing spreadcounts (sometimes there can be one scene rendering and other times there might be multiple scenes rendering at once. while scenes are rendered, new scenes can get added etc.)
now these changing spread counts do interrupt the writing (guess the loop is recreated?)
also no easy way to dispose the scenes when writing is done (the time based keep from my example is for simplicity only)

thats why i wanted to create the scenes and all the fuctionality in a class that can be instantiated and disposed in the first place.

i am still bit lost on how to tackle this task differently.

i have added texture writing to your patch to show the problem with this approach (despite having plenty of time for the task you will notice the writing failing to write the 10 textures in some folders. i have lowered the lfo period a bit so the scene spread count changes)

leak_workaround3.vl (78.4 KB)

Think i will need to create a fixed amount of scenes that act as “slots” for the rendering and processing. When a new task comes in, look for free slots and do the task there. Feels so wrong though, kind of like back in beta:(