Memory Leak in Screenshots

Just wondering if this has been looked at recently.

I am looking at examples like these:

which provide use of the Tools.CreateScreenshotUsingRenderTarget option.

Which for our project, it works great… but looking closer at the inspector I am noticing double instantiation of the engine when running. You can see that in the playground link above.

This seems to fire regardless if I start as webGL2 or webGpu. Then like in this forum post… the whole app bugs down to almost a complete halt. :cry:

Any ideas on what it would take a remediate the issue? Been bashing my head for a while now. Thank you!

Welcome aboard!

It seems the problem was because of a bug in a driver, so nothing we can fix on our end.

You can try the workaround from this PG, which doesn’t create a new engine.

Thank you for responding and the example.

I’m trying to wrap my head around the example spending a few hours looking. Maybe there’s just a lot going on in that example but to be honest, I don’t understand. I see the use of dumpData and the _screeshotCanvas creation but is there a reason why it is never fired? The console.log(“here”) never executes, was it supposed to?

Thanks again!

Does it mean that, for example, BABYLON.Tools.CreateScreenshotUsingRenderTarget(engine, camera, 100) will always create a new engine? There is no such issue with 6.49.0.

Yup as this is the way to not see anything in the main canvas during screenshot.

The PG doesn’t work anymore, because we changed the way we access the DumpData function. It used to be a static member of a DumpTools class, but it’s now exported differently, and I don’t know if it’s possible to override it… cc @RaananW who will know for sure.

In case it’s not clear, a single new engine instance is created and will be reused every time you call a screenshot function. I seem to remember that another reason why we had to proceed in this way is that the main engine canvas could be configured with pre-multiplied alpha, which messed up the screenshot.

This is 100% correct - it is not possible to overwrite it in es6 (because this is how modules work). What we can do is allow injecting your own DumpData function in CreateScreenshotUsingRenderTargetAsync, or internally allow for this functionality. That would be the simplest path forward, if this is a needed functionality

1 Like

This was my original report, so I can chime in. One thing that helped was switching to a Vulkan driver for the Linux machine. That lowered the overall GPU usage but did not eliminate the problem. The only solution was to cycle Babylon instances out after a set interval. This keeps the issue from causing disruptions.

Regarding the two options above, if the pre-multiplied alpha causes visual issues, could we use that as a flag to use the main engine if it is NOT set? Likewise, allowing for a custom DumpData function to be injected would be useful for these scenarios.

2 Likes

Here’s a PR that allows you to pass a custom DumpData function:

When it is merged, this PG will work:

2 Likes