[SOLVED] Force garbage collection?

Hi :slight_smile:

Is it possible to force the garbage collector to run? In C# we can call GC.Collect() or something like that, but I haven’t seen anything similar in PlayCanvas.
When we transition from the menu to the game scene, we would like to make sure we don’t have any garbage leftover and the best place to have the garbage collector do it’s thing would be during the loading screen where any stuttering wouldn’t impact gameplay.

Unfortunately, as far as I know, there is no way to manually trigger the garbage collector in JavaScript. (At least universally between the browsers).

That’s too bad. I guess we have to spend more time tidying up our code then :smile:
Thanks for the reply.

Does this mean that there is also no way to disable the GC, in favor of manually running the collector?

I’m new to JavaScript, but this technique was important for getting large projects to run in Lua.

1 Like

Yes, you aren’t able to disable the garbage collector nor control when collection happens.

So, yes, it’s a long journey of following JavaScript best practices to get decent performance and memory management.

1 Like

Forcing garbage collection means you have a memory issue and you don’t know what is causing it. Chase done the leak!

Same story with java code. A long time ago the Swing GUI of one of our projects eventually hogged up all the memory. I dug deep into optimising garbage collection. In the process, we found the leak in one of our layouting libs.

Quintessenz of the story: even if you tune garbage collection - you have to know what is causing your problems. And at that point you can fix the issue at the root :slight_smile:

Of course, memory leaks should be traced and killed.

The technique I mentioned involved disabling the GC altogether, pre-allocating all variables, and forcing GC calls in-between scenes (or at other “reload” moments). This saved us 8ms per frame in overhead, in the game I was making. But, this was in Lua, which permits full GC control.

1 Like

I guess a gc in a controlled environment (eg: in between scenes or fade out/in) could be okay :innocent:

This is not a Playcanvas issue but rather the way JavaScript works.

If you were asking for a Playcanvas internal function… I wouldn’t know of 1 :sunny:

Ugh! No control, at all, eh? Well, I can work with that. I know C++, so I should be able to manage.

Sounds like pre-allocation is still a good idea, though. I’ll keep on with that.

I would love to have more control of when GC is called for the reasons mentioned above :frowning:

Having a frame drop in a real time game is not a great experience.

@Cain_Quigley I’m going to assume that you are using object pools already to minimise allocations?

There are some code samples (I think the orbit camera code does this) where to minimise garbage, we use global variables to store temporary values such as vec3s which we can get away with as it is still single threaded.

Again, I’ve used that technique with Lua…was important to do with the GC disabled.

I’m still so new to JavaScript, though. I’m leaning on my experience with Unity (which has a similar editor), and Lua (which uses similar coding styles).

For now, I’m relying on objects placed in the editor…enabling and disabling, as needed.