Random lag spikes, cannot figure out why

Hello,

So I’m a fairly new programmer and I’m doing a project which is going to be physics based (not fully, depending on performance). Now I have a fully working prototype but I’ve hit a wall that I cannot get past. Before I go nuts building levels I want to get basic optimization down. The issue is that, while in a level, the game lags randomly. Without input, without movement, just idling. I cannot see why in the profiler, all I see are things like WaitForPresentInGfx or Semaphore.WaitForSignal when the spikes happen. I’ve attached 2 screenshots right when a spike happens.

On top of that, here’s some additional info:
With dynamic batching, the usual # of batches/draw calls at any time is 40-65.
SetPass calls don’t exceed 25.
Max tris on screen = 19k

Most performance heavy aspect: Upon ball collision, I have a script that uses the sphere overlap method to grab nearby rigidbodies. Then it assigns a new tag “Hit”, removes all constraints (so that gravity and ball force work. The reason they are constraint is because it’s voxel based and many voxels need to float). Then the ball applies force to them to blow them away. Now, according to the profiler, while there is a small spike when the ball collides, it’s not noticeable and this issue happens when idle.

All voxels (for example in level 1, 396 objects) get instantiated upon scene loading. Any objects already hit get destroyed. Once a level is done, a new scene is loaded if player chooses to go to the next level. This is done to save space as I’m planning 500 levels+.

All objects use the mobile diffuse shader. Everything except the voxels (because they move around) is static and baked into lightmaps.

I have no clue what else to do about those random lag spikes. Any help is very much appreciated.

Side note: Performance is always worse when running a development build. When running a release build game performs up to 30% faster.
Devices tested on:
Samsung Note 8
LG K30 - Extremely crappy phone, however performance is consistent (20-30 FPS).
Additional question:
Could it be the phone itself? Granted the Note 8 is quite capable, but I do notice that the phone itself has those mini lag spikes randomly. My question is rather, if the phone itself is hiccuping, will the profiler pick it up as well? It would explain how a crappy, crappy phone has solid performance (well, yeah, it’s half the FPS, but it’s steady) and mine doesn’t. Currently waiting for an HTC used from 2014.


@Archieonic This happens when you miss the frame rate threshold by just a little bit. E.g. a frame sometimes takes 16.9 ms instead of 16.7. The rendering then has to wait for the next 16.7 ms point, so you see those spikes.
Which Unity version are you on?

Thanks for the reply. I’m on 2019.2.15f1.

It was indeed my phone (or so I think). Perhaps due to not restarting it in weeks or too many apps bogging it down, it wasn’t the game itself. Once I restarted my phone and tried again, it worked as intended. I still get Semaphore.WaitForSignal and WaitForPresentInGfx, but no lag spikes.

1 Like

“WaitForPresentInGfx” just means “The CPU is done with its work, waiting for the GPU to catch up”

Interesting, I did read a bit about that, it was just confusing when that happened paired with lag spikes. For a physics based mobile game I thought the CPU would not have ample time to wait for the GPU. I’ve tried to keep optimization tight from the get go but then again, with one month of programming under my belt I’m always scared I’m going to find something later down the road that’ll require redoing a lot of things. I guess that’s also part of development, heh.

Thanks for your time!

it’s part of development, but it’s exasperating.
What you can’t detect, you can’t fix.

I have random latches in my game. It usually runs at 180 FPS and then again… 25 FPS. Well… if it was always in the same place you could investigate what happens, but it is not like that.

You go through that area again… and it doesn’t happen. And it’s not that instead of 25 there are 35, no. It’s just that it doesn’t happen. But oh…suddenly, in another place than the previous tests did not give a problem, WHAM!

It’s part of development, yes. But it is impossible to solve problems if you cannot detect where the problem is.

It is very demotivating. You do not know the reasons and, if you are a perfectionist, you will not move forward.

My computer is:
i7-11700k
64GB RAM
NVIDIA GFORCE RTX 3060 12GB
Unity 2021.3.7f1

They make you want to throw in the towel!!