[SOLVED] Random stuttering is giving me a headache!

The past few days were a nightmare for me. If a problem comes up, I usually fix it ASAP. The issue that I’m having right now, however, is just something else. It appears that either A) I can’t google, or B) I’m first to have it. Without further ado, here’s my problem.

3 days ago in the evening I compiled my project that I’ve been working on and instead of the normal, locked (not VSync, IsFixedTimeStep = true) 6.9ms per frame (measured in the Draw(gameTime) function) I was getting between 6.9 and 20.8 (average 70 FPS). It was randomly skipping up to 2 Draw() calls every Update(). First thought: I destroyed performance. Switched to IsFixedTimeStep = false to confirm my assumption. What do I get? 2000 FPS; dumbstruck. I restart it a couple of times – nothing changes. VSync set to true fixes the problem, but in an action game it’s a terrible idea. I even change the target elapsed time. 60fps and I get 40. 30 worked fine. 100 – 70. 1000? I get 1000. Confusing as ****. Probably something screwed up in the OS or something, good ol’ restart should fix it. And fix that it did. I didn’t pay it much thought after that, until…

Day 2 can be summarized with one word: consternation. The problem’s returned. And no restart can fix it. Tried creating an empty project. Literally nothing there except for a SpriteFont, SpriteBatch and a DrawString() showing FPS. Exactly the same behavior. I tried everything from build type changes to registry edits. Nothing helps.

Day 3 is exactly the same, except for it fixing itself right before I went to sleep. Today started off alright, but it just broke after 5 runs or so. I don’t even know where to begin in my search for the fix.

My partial solution for now is just running it unlocked (FixedTimeStep = false), but I do want a frame limiter in the end (not everyone wants a game to hog up an entire core or two), so sooner or later I’ll have to ask for some help. Of course, it’s not guaranteed to happen on other machines. I just tested it on my crappy laptop and it’s working flawlessly, so I will not worry about it too much. It ain’t that urgent, but it is still an issue I’d like to get fixed, just for the sake of being bug-free.

TL;DR
3 days ago all of my MonoGame apps just broke for some reason. They’re dropping Draw() calls like crazy if the framerate is locked (IsFixedTimeStep = true, VSync = false, TargetElapsedTime = 6.944ms), else they’re running in the thousands of FPS. The problem periodically fixes itself and appears again for literally no reason and nobody has ever had it yet (or they weren’t vocal about it or I can’t Google). Here I am now, asking for help or at least some ideas on where to look.

EDIT: I forgot to add some some info.
MonoGame 3.5, .NET version 4.5 and higher (all tested and broken).
Inside VS environment and outside – both broken.
x86, Any CPU, x64, release, debug – all broken.

Specs:
AMD FX-8350
Radeon HD 7850
16GB DDR3 RAM
Win10 x64

I can add more info if I forgot anything here.

Hi !
Why this ?
Did you update something (monogame, OS, radeon’s drivers) ?
Did you install a new software (AV, backup solution etc) ?
Can you try with the dev branch ?
Do the stutters also happen in a game not related to monogame ?

I didn’t do anything besides changing some code between build #1 (fine) and build #2 (broken).
I guess I’ll try the dev branch.
No, this is a first.
EDIT: I just remembered something. Yup, it does match with one thing. I was trying to develop a Windows Console (you know, cmd.exe) game engine thingy and I had weird hiccups with Thread.Sleep(). Since that time I learned that you don’t use Thread.Sleep() for time-critical stuff due to some Windows-related stuff.

Did you add a new model ?
Got the same sort of problem when I added a model which was really badly optimized/not optimized at all.
If you revert your changes is it still stuttering ?

EDIT: Yes, Thread.Sleep is not the best way to handle timed stuff. But when you quit the console, the corresponding thread attached to the process should be closed too (cf in taskmanager) if, at least, the thread is set as background.
But I don’t think it comes from this.

First off, it’s a 2D game. Second: fresh project with an FPS counter has this issue, and all the other random ones that worked just fine 4 days ago.

Alright, dev build tested. Empty app with FPS counter has same behavior as before.

Give me a break
I won’t be able to debug anything right now, because it just fixed itself. Again. It’s probably going to come back soon, so your ideas are still welcome, but I can’t test them right now. Thanks in advance.

You should monitor which processes run when it happens / doesn’t happen to maybe find the guilty one

The solution is to apply a fix like #4207 in monogame source or set the timer frequency yourself by pinvoking NtSetTimerResolution.


I suspect that some program was setting the timer while you were having smooth frame rates.

Your other options is either set VSync=true, where the gameloop Thread.Sleep() is rarely called (Present() syncs the game loop in a more stable fashion), or use IsFixedTimeStep = false where again Thread.Sleep() is not called.

@nkast Alright, thanks for that. Next time I get this stutter I’ll try PInvoke. It’s feels hacky, but it’ll do for now. I don’t want to mess around with the source. I’ll just remove the hack when 3.6 comes around with the fix.

Thanks again :slight_smile:

This tool might be helpful to test your game.

close all other apps (browsers, etc) until you get 16ms and watch your game’s fps.
Then set the timer to 1ms (or whatever the default value is in your system).

@nkast Bingo. I set the timer resolution to 1ms and it’s working like a charm. Lovely. I appreciate your help a lot. Didn’t even occur to me that something like this exists, let alone it being so easy to fix.