FPS drop at regular intervals

@reiti.net

Alright, so I followed this guide: https://www.tech-recipes.com/rx/37272/set-a-programs-affinity-in-windows-7-for-better-performance/

And made my game launch with:

Priority: normal
Affinity: 1
Result: Game runs choppy; yet framerate counter claims that it’s 60FPS. Game doesn’t irregularly stutter; even at the usual 1700 frame that I expect it to (probably because it’s stuttering nonstop).

Then I set:

Priority: high
Affinity: unset
Result: Game runs smooth like butter, yet the stutter at around frame 1700 comes back.

Then I set:

Priority: high
Affinity: 10 (Core 5)
Result: Same as the first test. Nonstop choppiness, yet framerate counter claims everything’s fine.

Does anyone know what’s going on with the FPS counter? Is it to do with the MonoGame timer issues mentioned above?

I’ll be honest I don’t quite know much about how Cores work, so if I’m setting the wrong affinity’s here, please let me know and I’ll continue the tests.

Also, I doubt this is related, but if my game isn’t in focus, the FPS drops to 30. This is however normal behavior for my computer (it does this with most games I play).

Please check whether there is any usage of TargetElapsedTime. If you set it and not using ticks, this cause stuttering too.

Ohh, interesting. I am using “ElapsedGameTime.TotalSeconds” (for the camera/objects movement) from the GameTime class and I do not use ticks (since I don’t know about that). I will try to apply these changes, let’s see how it goes. In fact, question: how am I supposed to use “ticks”?

Oooh! Alright, I’ll have a look… So it turns out, I was using:

Global.deltaTime = ElapsedGameTime.TotalSeconds.

I’ve changed it to:

Global.deltaTime = ((float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond);

After which, I did several trials. Here were my results (IsFixedTimeStep and SynchronizeWithVerticalRetrace are false for all of these):

  • TargetElapsedTime unset. Game stuttered at around frame 1700.

  • I set TargetElapsedTime to 60fps (in Ticks). Game ran once without ever stuttering; then began stuttering again when I closed and re-opened it (this happens all the time. I’ll randomly “luck” out and get a stutter free run).

  • I set TargetElapsedTime to 120fps (in Ticks). Game ran at a smooth 120 FPS, but then stuttered at frame 3400 (which is double the 1700 frame that it usually stutters on in 60FPS). I guess from this we can tell that the stutter is based on time, not frames.

  • I set TargetElapsedTime to 20 fps (in Ticks), just to see if maybe giving each frame more time to execute would solve things. I could not get the stutter to occur.

  • I set TargetElapsedTime to 30 fps (in Ticks). Couldn’t get the stutter to occur.

  • I set TargetElapsedTime to 45 fps (in Ticks). Stutter occured.

So… what do I make of this? Well, whenever the stutter occurs when I run the game in 30FPS+, the FPS dips below 30FPS, yet when I limit the game to 30FPS, I guess it’s got more time inbetween to catch up…? That, and it the actual FPS doesn’t seem to matter, because the stutter will always occur at around 30 seconds in regardless of how many frames have been processed. Any ideas?

I’m not 100% sure this is what he meant, but I think he means rather than getting TotalSeconds from your GameTime class, use GameTime.Ticks instead and divide the Ticks by a specific number to get the total seconds value. My game does this for example:

Global.deltaTime = ((float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond);

(Though, I’m not sure if that’s exactly the way EnemyArea meant to do it. We’ll have to wait for his comment).

Hey guys, its the TargetElapsedTime in the game class. Check wether it is set to any value or not. If it is, comment it out und retest the game. please take a look here: https://github.com/willmotil/MonoGameUtilityClasses/issues/1

Hey EnemyArea. Thanks for the advice!

I’ve tried leaving TargetElapsedTime unset and Console.Writeline-ing its value. It seems that even if I never assign to it, it gets set to 00:00:00.0166667 by the MonoGame backend. So I tried setting:

TargetElapsedTime = TimeSpan.FromTicks(166666);

In my Game1.Initialize() method like you showed in your link. Aaaaand… it seems a bit better? (I’ve gotten several sessions of my game in where stuttering never occurred, and several where it stuttered). Did I follow your instructions properly?

I’m beginning to suspect this is a hardware issue with my computer, because the more I close and re-run my game, the higher the chance of it stuttering seems to be. Could there be a memory leak when I’m closing it?

Precisely why you will see me question hardware specs as a first diagnosis…

If you are seeing performance penaltys after restarting your game over and over that is likely some texture memory on the gpu that is not being released properly.

Restarting your computer would wipe the gpu memory and should entirely fix it that is one way to confirm it.

If you wanted to see for sure and investigate it in real time you could use a gpu monitoring tool like MSI Afterburner which is free ( https://us.msi.com/page/afterburner ) if you wanted to see if the gpu was indeed filling up with extra memory and any spikes that were occuring.
It has a hardware monitor and one that you can actually overlay on the screen i have no clue if that part would work with monogame but the hardware monitor will give you all kinds of info.

It should eventually release the memory on its own over time after you close your app and as you run other programs as well so its not a big thing but ya its a sign of some video memory not being freed on app close.

There is also System Internals if you really wanted to dig in to the problems or if you think there is something going on with mg. https://docs.microsoft.com/en-us/sysinternals/downloads/system-information , https://docs.microsoft.com/en-us/sysinternals/downloads/rammap

How to see what is affecting cpu time… and other things using system internals.

This is also a good breakdown of process tasks threads ect… with none other then Mark Russinovich so basically from the horses mouth.

msi

1 Like

Have you tested running a release version of your game?

With Debugging Mode and VS attached, my (not limited) framerate is often less than half of the release version. And 60FPS cap is almost rockstable on release build than with the debugger

Give it a try just to see if the release version have the stutter as well.

1 Like

Out of curiosity, does this occur in other games? retail games from other developers to be more precise…

1 Like

Hey all! I’m back.

So, before I continue, I want to mention this. I tested my game on my work computer. It’s specs were:

OS: Windows 10
Processor: Intel® Core™ i7-4790 CPU @ 3.60GHz 3.60 GHz
Installed memory (RAM): 16.0 GB
System type: 64-bit Operating System, x64-based processor
Gfx card: NVIDIA GeForce GT 740

I tested the release of my game 5 times on that machine in succession, and the stutter never occurred!

For comparison, my machine’s specs are:

OS: Windows 7 Ultimate
Processor: Intel® Core™ i7-6820HK CPU @ 2.70GHz 2.70 GHz
Installed memory (RAM): 16.0 GB (15.8 GB usable)
System type: 64-bit Operating System, x64-based processor
Gfx card: Intel® HD Graphics 530 & NVIDIA GeForce GTX 970M

@reiti.net, I’ve tested the release of my game on my current machine (the one which has stuttering), and it still causes the stutter at the usual times, and still only sometimes when I run it. However, when it does stutter, it’s more of a brief “jump” (the freeze is a lot shorter). It’s also not being picked up by my ingame FPS-stutter-detector.

@MrValentine, I don’t so much get stutter, but I do get 30-ish FPS with a lot of demanding games (the new Wolfenstein 2 for example didn’t have the best framerate at intense areas). However, I can’t say I’ve gotten just the sudden “stutter” like in my game out of any game I’ve played on this. However…

I’ve also tested my game on my friend’s gaming machine. His specs are:

NVIDIA GeForce GTX 970

I saw him running the new Devil May Cry 5 the other day and it ran pretty smoothly (better than my laptop could manage). Definitely didn’t notice any stutter. Yet when he tested my game, he got the same exact stuttering as on my machine.

@willmotil I’m thinking your theory is the likely cause for my case. I’ll download Afterburner, do some tests, and report back.

@Paolo_Ferri How are things on your end? Did you try the Ticks fix (and did it work)? I feel bad for stealing your thunder like this; I was just trying to add more information to this investigation.

I was about to ask if you were using Optimus but then, is your friends system also a laptop? I remember reading NVIDIA Optimus causing stuttering in the past…

Added these in case they help…

EDIT

Try playing a game your laptop can play at 60+ fps, playing one that it can barely run at 30 is not really a good test point…….

I have, and yes there is some improvement but the problem is still there.

I haven’t tried that, however, I mentioned before that I saw this happening in other imported Monogame projects (I tried the bepup physics project).

I had that already set up, in milliseconds:

TargetElapsedTime = TimeSpan.FromMilliseconds(16)

I tried setting that using ticks but nothing, still stuttering.
One thing I noticed is that it’s clearly dependent on other processes: if I open 5 tabs of Chrome (famous for its hunger for RAM), the stuttering is clearly more noticeable.

1 Like

Well alright. I tried it with Hat in Time (Unreal Engine game). Runs smooth with no stutter. Also, while I know the game is ancient, Age of Empires 2 HD has also been a recent game I played which runs just fine. I’ve also tried uninstalling + re-installing my NVIDIA; but that hasn’t really changed much. Oh, and my friends computer is a desktop. Of all the tests I’ve done, the only laptop has been my Windows 7 machine.

So… on a hunch, I tested my game on another family member’s Windows 10 machine. No stutter. This leaves my standings at:

Windows 10 machines: 2 tests, 0 stutter.
Windows 7 machines: 2 tests, stutter on both.

Could it be Windows 7?

@Paolo_Ferri, would it be ok with you if you PMed my a build of your game? I’ll test it across my machines and see if I get the same results as with my game.

Are you using optimus?

EDIT

or, try sideloading Windows 10 on your system?

I think so, but my friends also stuttery computer wasn’t. As for Windows 10… I’d really rather not install that… I’ll do some more research into Optimus and get back to you (gotta run now). Um… would it help if I private messaged you a simple build of my game?

Windows 7 isn’t really supported much longer so, not much use unless you test on modern systems, I think they are going to openly offer the free upgrade again soon, unfortunately I don’t have much time to test right now as I tend to be extensive in testing…