Now before you scream “Garbage Collector!” hear me out.
I know garbage collection uses a lot of recources for the moment when it decides to dispose all that trash. Yes, that was a problem for my game, too, but I am not sure it’s the root cause of my current issue. As a matter of fact, calling the GC more often helped with the issue. I monitor GC memory and when it disposes etc. and it does not seem to be correlated with my frame skip problem now.
Ok so here’s the deal:
It appears the game will sometimes draw 2 frames right behind each other and later have one frame be double the length of a normal one. Frame pacing. And all of this is happening more of than garbage disposal.
This is not an issue if my game runs at 300Hz and I manually set IsFixedTimeStep = true. → 60Hz
aka like this
if (GameSettings.LockedFps) { _graphics.SynchronizeWithVerticalRetrace = false; //this.TargetElapsedTime = TimeSpan.FromTicks(16666); //is 60 by default anyways IsFixedTimeStep = true;
}
The game will appear to run smoothly, however I should note that when using VerticalRetrace==true aka Vsync it will sometimes (every second or two) still register a frame drop and set the speed to 30 for a few milliseconds.
However, if I increase graphical complexity, for example by using SuperSampling, I can easily push the framerate to go below 60 Hz without a limiter.
And now it’s really janky, every half second or two it will skip a frame, if I run at 50 ms it will go
50 50 2 100 50 50
and sometimes
50 50 2 50 50 100 50 2 50 50 100
although I am not sure maybe I am recording that in a wrong manner somehow.
Now comes the part that initially made me think it’s GC.
I set up a StopWatch around the functions to try to understand what’s so janky exactly.
I calculate an avg. time over 100 frames, the average offset/variance over 100 frames and a maximum offset/variance.
Turns out EVERY SINGLE FUNCTION has a maximum offset several times the average time.
So if my function takes 100 ticks, and i have an avg. epsilon of 20 ticks i have a maximum eps of 400 or so.
These offset frame times, looking at a single function, correspond with the frame peaks but will peak so extremely only once every 10 seconds or so, which leads me to believe it’s another thing that’s stalling the CPU.
Ideas?
Just to add, I have a pretty bad multithreading culture going on, where the physics thread writes to the same position values the draw method in the main thread has to read and i just hope it works and somehow it does. But could it be something with that?
I don’t use any threadcontrol aka stalling or anything