Chris Ashworth

Mountain

Common scenario: You're in a playtest session of your multiplayer Unreal title, it's all going well, but suddenly...."has the server died?", "I'm stuck!", "I'm lagging"....and then "Oh, no, it's fine again now".

You'll more than likely come across this scenario several times during the early phases of a project's development. Particularly working in AI, this is something I've had to fix more than a few times, as there's a common culprit that comes from Unreal's AI system.

If you're taking profiling snapshots of your server builds, it's easy enough to see the problem: You're churning UObjects, that is, something is creating new UObjects every frame, and every 30 seconds or so, the garbage collector is having to clear up the thousands of accumulated objects, which gives you this huge hitch on the server.

In order to find these issues, there's a useful option in Editor Preferences -> Show Frame Rate And Memory. This gives you a little extra information on the title bar of your editor window, showing framerate, memory usage, and critically, the number of UObjects.


Normally, when you're playing your game, you'll see this object count climbing rapidly as the game loads and everything initialises, and then fluctuate up and down, but settle in a steady range over time.

If you have a churn problem however, the number will be flying updwards like the altimeter in a crashing place, but the other way! It's very easy to spot so having this editor option turned on by default and catching it locally will help prevent this ever being a submitted issue. Obviously there are other tools such as Stat UObject to find more detail.

To then find out what the offending UObject is, you can simply stick a function breakpoint in UObject::UObject(). Once you hit it, continue a few times to make sure you haven't picked up something that is legitimately being constructed that frame. 

Chances are, if you have AI in your game, the culprit will be UAITask_MoveTo. This is the object created when the behaviour tree system (with Gameplay Tasks enabled) requests a new move. Early on in the development of your title, it's likely the AI configuration is pretty rough, the navmesh isn't always up to date, the world isn't marked up correctly, and for whatever reason, you have an AI agent that has got into a position where it can't move, and every time it requests a new move, it fails. Every frame. 

Ideally, your AI never gets properly stuck like this, but even so, it's a good idea to put the MoveTo task into a Selector node with a Delay after it, so if a move does fail, the agent waits a second, before trying again. No more new UObjects every single frame!

Obviously the issue won't always be AITasks. UI is a common culprit, as it's very easy to make interfaces that inadvertently create new UMG widget containers every frame. Fortunately though, whatever is causing UObject churn, it's dead easy to identify, track down, and fix!