Hi,
I’m encountering an issue with properly cleaning up the Speckle Viewer. Every time I open a new model on the same page, my GPU memory usage increases, which suggests that resources are not being fully released. Over time, this leads to significant memory accumulation.
I’ve already taken the following steps to clean up the viewer:
viewer.dispose(); // looks like it does nothing in the source code
Despite these efforts, the GPU memory footprint continues to grow with each model reload. Should I manually clear the scene, materials, or other resources? Are there any best practices for fully releasing GPU memory in Speckle Viewer?
I’d really appreciate any guidance or suggestions.
I’m not experiencing anything like you are describing at least when running the speckle web app or sandbox.
Typically, VRAM is less “elastic” than your traditional program heap. There is also less (if any) direct control regarding allocation especially when running WebGL. The reason why is that each WebGL implementation does a lot of behind the scenes data validation and API calls in order to keep things nice. That is not to say, you cannot mess it up. You can easily do so by pointlessly keeping resource handles alive.
Generally, unloadAll or unloadObject should be enough. However there might be edge cases that we are not treating well and this might not get cleaned up properly. Are you using a multi-viewer setup? Are you spawning and destroying lot of viewer instances? Can you provide us with a simple example that reproduces the issue? You can use stackblitz to make a live example
I see that you are creating a new viewer instance along with everything it entails, each time the refresh button is pressed. This means that a new renderer and finally a new webgl context is created each time. Because you are making distinct viewer instances, new resources are allocated each time. This makes you see the memory increase after each refresh
However, as long as you don’t intentionally keep all these viewer instances alive, they will get swept and their associated resources released. At least that’s what I’m seeing when running your example.
What we could do is implement the viewer’s dispose function where we would further delegate to three’s dispose functions. Maybe internally it deletes all buffers, which triggers something at driver level that reports the memory as “freed”. We will see to implementing the viewer’s dispose. There is no reason to recreate everything each time you want to load something in. Nothing is stopping you from creating new viewers, but a single one can load and unload as many models as you need. I understand that this was a test and maybe your real application might not do this, but mentioning it here so it’s clear
Yes, this is just a test case to demonstrate the issue.
In my real scenario, the viewer is just a part of the page, but unfortunately, I have to remove it from the DOM when it’s not needed. However, the user can open it again at any time.
At the moment, this is not a critical issue for me, but it would be great if, in the near future, there was a method to fully clean up all resources when needed.
Would it be okay if I leave this topic open? That way, whenever there’s an update regarding the dispose method, you could simply reply here and I will be notified