Force Garbage Collection

Hey,

So I’ve got a game in which a lot of actors are spawned, at a rapid rate. They also get destroyed almost as fast.
Since the latest Rocket upgrade, I seem to get a massive slowdown after about a minute, until a garbage collection happens. We’re talking of an fps drop from 120 fps to ~20fps, then straight back up to 120. Looking at the object count at the top of the screen, we’re talking going from ~300k to ~450k until the gc comes into effect.

I can force a garbage collection by using the console command, ‘OBJ GC’, but I can’t find a way to automate that through script. When I execute a console command, using OBJ GC, I just get a crash.

Is there any way to modify to GC rate? Preferably through just script.

Cheers,
Chris

Dear Chris,

The title of your thread, in World.h :slight_smile:

#Running When You Want

You could run it on a timer :slight_smile:

//void ForceGarbageCollection( bool bFullPurge = false ); //I use True
GetWorld()->ForceGarbageCollection(true);

#Crashes Solution → IsVallidLowLevel()

For actors that could be garage collected,

you can do this check

if(!Actor->IsValidLowLevel()) return; //dont try to access null or is-being-destroyed
Actor->DoStuff();

I find that catches a lot of what would otherwise be crashes when GC has just been forced

Rama

1 Like

You should never ever allocate and destroy anything rapidly “At a rapid rate” - If you are doing this then you should be using a spare list. And reusing those allocations.

Also you cannot simply force a garbage collection any time you want. There are specific points in the update loop where it is possible.

GEngine->ForceGarbageCollection();

This occurs after all the tick groups have run. so NO you cant run it on a timer. You tell it to ignore its heuristic, and force a GC at the end of the current frame.

1 Like

There’s a handy console command that will force garbage collection every frame. This is really useful for testing to make sure that you didn’t muck up your memory management:

gc.CollectGarbageEveryFrame 1
6 Likes

Why no one suggested to use object pooling?

If you spawn and destroy these actors really fast then you have to use object pooling

It was mentioned in the 3rd comment.