Small freezes / fps drops with increased frequency when speeding up game

Started by RayZoar-Z, July 10, 2019, 06:05:10 PM

Previous topic - Next topic

dragonid1423

I suppose the best we can do is say that the GTX 960 somehow is the source of this. It's probably a jump to assume, but I'm out of options.
Sometimes I can hear the screams of the alpaca. I'm serious, please help me they're fighting again.

dragonid1423

I tried poking around on reddit for some more info on this, and I found someone who was having the same issues with a GTX 1070, so the 960 is ruled out as the culprit. While that's good, it's certainly not great because now we know that the issue isn't caused by a known problem, which forces us to go back to the drawing board.
Sometimes I can hear the screams of the alpaca. I'm serious, please help me they're fighting again.

dragonid1423

For anyone finding this thread down the line: The fix is stupidly simple: Run Rimworld in Administrator Mode.

Thanks for helping everyone!
Sometimes I can hear the screams of the alpaca. I'm serious, please help me they're fighting again.


jay124

Sadly, I've been experiencing the same issue and running it as Admin did not help alleviate the issue. I was hopeful it would, but didn't. :(

It's the exact same thing this person was having.
https://www.reddit.com/r/RimWorld/comments/consfj/stuttering_every_couple_of_seconds_due_to_memory/

But in my case, it looks like it runs GC at 700MB and drops down to 500MB every 6 or so seconds.

RayZoar-Z

Administrator mode also doesn't help for me. I gave up on finding a solution since my last post and haven't played since, but if anyone finds anything please post it.

madd_mugsy

I've also been having this problem; it started around the 1.0 release last year.  I never could trace it down, but it becomes more obvious the more mods you add.  All I need to do to test it is start a new game and speed up time and watch a river freeze up and the FPS drop briefly every X seconds.  It happens in vanilla too, and it doesn't take many mods for it to become much more noticeable.  I reinstalled my OS entirely over the summer (finally upgraded from Win7 to Win10), have none of the same hardware as you (also a 1070GTX like the guy on reddit though) so I'm pretty sure it's a game thing.  From the descriptions in this thread, it sounds like an issue with the unity garbage collection mechanism. 

Edit: It's definitely the GC in game.  The RuntimeGC mod lets you watch it and the spikes correlate exactly with when the memory is automatically reclaimed.  I had a similar experience to RayZoar-Z with it.

The solution is likely a coding change to the base game.  One idea would be to prevent the GC from running automatically / as frequently and force it to run when a dialog is displayed instead.  I don't know if this kind of base level code change is possible via modding.

Alternatively, the issue could be caused by a memory leak, causing the GC to run frequently in an effort to try to keep up with the leak.  Again, a base game fix though.

Unfortunately, this issue makes the game super frustrating to play.  I haven't played it in months because of it, and was hoping to find that this had been figured out by now.

My best guess is that unity was updated to a newer version when Rimworld moved to 64bit (since that's when I first noticed the issue and started seeing other people post about it) and that this newer version somehow altered the way garbage collection works, or introduced a memory leak, though I could be incorrect.

LWM

One other possible thing to try....

RimWorld does run on Linux.  You could get a (large?) USB drive and install a live version of some lightweight Linux on it, and see if you can fit Linux+Steam+RimWorld on it.  The garbage collection engine might behave differently.

I have read a little about the C# garbage collector, and it may be possible for someone to suggest a garbage collection on a faster/different schedule.  But I'm not 100% sure.

One other thing to consider: upgrade your system's memory, if it's low and you use a spinning HDD?

I sometimes get stuttering, but it's usually because I have 8 or 9 Firefox windows with 50-60 tabs open in the background :p  Closing those helps a LOT.  Good luck.

madd_mugsy

Nothing else running, Rimworld's on an SSD and I have 32GB of DDR4 RAM.

I'm going to keep experimenting.

LWM

Well, THAT should be promising.

One reason I was thinking Linux was b/c you would be (no choice) using a different set of hardware drivers...

--LWM

madd_mugsy

So I've tried a bunch of things, but nothing has really had any effect on it.

It's much more pronounced and faster to reproduce with mods running, so I've been running a dozen or so to make things easier for myself.  RuntimeGC is key though.

You can set up RuntimeGC to display the current .Net VM memory usage in the toolbar (and in the main menu).  In my case, it caps out around 560MB and then the garbage collection kicks in and it drops to 442MB. 

What's interesting is that this VM memory usage keeps going up, even in the menu or while the game is paused.  It goes up faster for each speed setting in-game, which is why the stutter happens more quickly at 3x than 1x.  What's unusual is that it is going up at all when things are paused and nothing is happening.  Even more so that it does it in the main menu.  This signals to me that there is some kind of leak happening somewhere -- some objects are getting constantly created and destroyed.

It would be interesting to see what other people see in the memory display in the RuntimeGC mod setting screen off the main menu. (Check 'Replace "RuntimeGC" with memory usage data' and set the update frequency to 'Realtime'.  Also uncheck "Replace/enhance vanilla GC" to view vanilla behaviour instead of the optimized collector in RuntimeGC.)   Is your memory usage also steadily increasing?

Edit: With no mods but RuntimeGC running with the settings changes from above, I can see the GC correlated stutter at 98MB before it drops back to 72MB in the main menu. 

I also tried unsubscribing from everything on steam and removing all manual mods, as I noticed they were still being referenced in output_log.txt, but it didn't make a difference.

I'm going to try it on a few other PCs and see what I get.

Edit 2:  3 other PCs exhibit the same behaviour.  The memory goes up continuously in the main menu from 72MB to 98MB before being reset.  When the reset occurs, the FPS counter drops 1-2 FPS on 60Hz monitors or between 10-20 FPS on 144Hz monitors.  This might be why only some people are noticing it.  It's much more noticeable at 144Hz than 60Hz.

I'm now poking through the source code for the RuntimeGC mod.  Since he was able to replace the GC with a more efficient one, I'm wondering if I can slightly modify it so that the upper limit / time before the reset can be increased to reduce the stutter, and/or what else I can do.

Edit 3: There doesn't appear to be anything obvious in there to control the timing of the GC unfortunately.  It looks like the setting to replace the vanilla GC just prevents the vanilla GC from doing any pawn cleanup, as it's already handled by the mod, but it still fires at the same time.

The good news is that it looks like we can just use the old System.GC object, since RunTimeGC is able to call System.GC.Collect().  I thought we were looking at some unity methods for GC, but since we can use the .Net object, we should (in theory) be able to reconfigure the GC settings via a mod.

moyashii

I'm having the same issue as all of you. I've nearly gone insane from looking everywhere possible to solve it or at least see if anyone else had the issue. It doesn't even seem that rare, I've seen quite a few people report having the same issue except they never get "official" replies, or ever seem to get it fixed.

I'm beginning to wonder if everyone has this problem but the majority just doesn't notice it. It's so strange and I've already done every possible "fix" short of reformatting to no avail. Deleted the game, rebooted, deleted and unsubbed from all mods, deleted the ludeon folder and disabled steam's syncing, updated drivers, etc.

It honestly makes the game completely unplayable and not enjoyable for me, despite wanting to play it so badly. It stutters in vanilla as stated in this thread before, but it's a lot less pronounced. You can still clearly notice it though, even when you're distracted, when you're building things and panning your camera around for example, it's very noticeable and can cause you to mess up your construction plans often.

The worst part is that hardware seems to not be the culprit. Despite this, the few replies the posts claiming to have this issue get usually blame it on the specs or something on the individual's side, such as poor mod orders, when it's clearly more than that. Even with 10-20 mods the stuttering increases a massive amount, from the reasonable every 20-30 seconds in vanilla to every 6-12 seconds. Not even big mods like Psychology or CE either. Not that it matters, because big mods seem to affect the stuttering just as much as small ones, so long as you have more than 10-15 added. There are others with far more mods, and far worse specs that are able to run the game at a smooth 60fps all the way up to massive colonies, and claim to never have this sort of stuttering.

I've also used RuntimeGC, and noticed what's been stated multiple times about the memory problem. I wish I was able to simply remove all mods and not have this stuttering, or could blame it on something so that I'd be able to fix it by just getting rid of it and be on my merry way, but it isn't the case. It's clearly something I am, and most likely most users, unable to solve on their own. Running the game on administrator mode did nothing on my side too.

This is frustrating since the issue seems to only have appeared a year or two ago tops. I've played this game ever since the early alpha/Kickstarter days, and I've always been passionate about it. So it really saddens me that I'm unable to enjoy it because of something like this. I hope that a solution comes out of this, or at least for it to get some light.

Edit: Formatting and stuff.

madd_mugsy

So while we might be able to do something using a new GC mod, I don't think it's a good solution. It doesn't really address the underlying problem - that something is creating lots of objects under the hood that need to be destroyed. Altering the GC would be a band-aid at best, and cause new issues at worst.

I have a hypothesis about what is happening.

When Rimworld moved from 32 bit to 64 bit, the unity engine was upgraded to 5.6.5f1.  My hypothesis is that there's a problem with that version of unity.

In the changelog for 5.6.6 of unity, I found 3 mentions of fixes to memory leaks. In managed code, a memory leak will be cleaned up by the GC, but can cause a performance issue, like we are seeing.

For reference -
https://unity3d.com/unity/whats-new/unity-5.6.6

There are likely more in further versions as well, though I haven't read past 5.6.6 yet. I'll do that if my hypothesis is proven.

So, to test this hypothesis, I propose the following experiment:

Install Rimworld B18 and the RunTimeGC mod for B18. Alter the settings of RunTimeGC as per my previous comment, and then observe the memory usage and FPS within the options screen in the main menu.  Note the version of unity in the output_log.txt file.

If the memory is stable, then we try B19.

IIRC B19 was when the 64bit switch was made, so B18 should be 32bit and on the old version of unity.

If it doesn't happen in B18, then we can make a couple of videos/gifs and send them to the powers that be showing them the difference, and request a unity version update to at least 5.6.6.

If it still happens in B18, then I'm going back to the drawing board...


EDIT: 

Ok, so first of all the B18 version of RuntimeGC doesn't have the memory bar in the options menu.  So I had to recompile the 1.0 version against the B18 unity & c# dlls to get that back.  You can find it here if you want to try it:

https://www.dropbox.com/s/idjk3p6cogn315l/RuntimeGC%201.0%20to%20B18.zip?dl=0

Note that it probably won't work in the game itself, as I had to comment out a whole bunch of features that aren't supported in B18, like battle logs and a bunch of the corpse removal stuff.

The memory usage in B18 _does_ still increase in the main menu, though there are differences.  For one, the memory goes from 22MB to 28MB before the GC kicks in.  Another difference is that on my 144Hz monitor, the game starts running at 137 FPS, not 144, and when the reset happens, it drops to 131 FPS.  So it's a 6 FPS difference, not 20.

While interesting, this doesn't really prove my hypothesis, as the memory still goes up in the menu in B18 too.

Unity version for B18 is 5.6.3p1.

I'm not sure what to try next...

What I'd love to try is recompiling Rimworld against Unity 5.6.6 or 5.6.7 and see if the issue persists, but that's not gonna happen.

EDIT 2:

So I've been playing around with things a bit more.  I discovered that I can swap out the UnityEngine dlls for the ones in Cities Skylines (5.6.6f2) and it still works.  No clue why, but it does, lol.  It should need a recompile or something.

Anyway, it didn't matter.  The stutter still happens.

After reverting back to 1.0 from B18, I somehow broke the steam FPS counter for Rimworld (and only Rimworld), so I started using Fraps, which indicates the FPS drop at 6-7 instead of the 20 or so I was seeing with the Steam counter.

I also starting playing with a mod called FrameRateControl.  This mod lets you force the framerate to 60 or whatever.  This reminded me of something that I forgot to note yesterday - on a 144Hz monitor, the memory usage increase will be much, much faster than on a 60Hz monitor.  For instance, at 144Hz, it goes up from 72-98 in 16 seconds (1MB/s).  At 60Hz, it's more like 0.1MB/s, so the GC starts its process nearly 10x later theoretically.  I say theoretically because I noticed the memory usage cycle is not linear.  It gets faster as it gets closer to the upper limit.

So I tried 300 FPS using FrameRateControl.  It's about 50% faster than 144 FPS.  In game is where it gets interesting...  It's about as noticeable as 144 FPS, but the speed of memory increase is the same regardless of game speed.

Conversely, if you force the FPS to 60 in game, the stutter happens less often, is linked to game speed and appears to be less noticeable, as it only happens every 40-60 seconds or so... though I haven't tried it yet with mods.

EDIT 3:

I added a dozen or so mods and tried it out again.  With this setup it's a noticeable ~1 second stutter at 60 FPS (drops to 54 in Fraps), and a ~1 second stutter at 309 FPS (drops to ~280 in Fraps; I don't know why the 300 FPS setting in FrameRateControl runs at 309).  However, the stutter occurs every 3-4 minutes at 60 FPS and every 1 minute at 300 FPS.

I just noticed that there's a "No Limit" option in FrameRateControl which makes it go up to ~1450 FPS, and then drop to 1200 FPS when the GC cleans up. 

I'm going to mess around with it some more.  30 FPS might be a workaround as the GC would probably only run every 10 mins or so... Nevermind it's not.  The memory builds up super fast anyway when you increase the speed of the game.  The stutter is noticeable regardless and the FPS doesn't have much of an impact on how long it takes for it to occur in-game. 

Btw, for reference my main PC:
Intel i7 4770k @ 3.5GHz
32GB RAM
1070 GTX
Samsung 850 Evo SSD
Win 10 Pro, recently installed from scratch a few months ago

This has been an interesting experiment, but I'm not really sure what to try next.  I can't prove one way or another that it's a unity problem or a game problem.  I can see that it happens on multiple PCs, but it's more pronounced for those with more mods and/or faster monitors, and it's definitely related to GC process.

Any ideas, folks?

Penchekrak

Probably, I have the same issue. My thread here with video demonstration: https://ludeon.com/forums/index.php?topic=49577
Since I have Linux systems on my work, I will try Gallium HUD to capture frametimes and check if issue is on Linux version too.

UPD: I was unable to run Gallium HUD, so no frametimes graphs. I have run Rimworld on Ubuntu system with Steam FPS counter and clearly saw the same issue - FPS drops from 60 to 56 in a loop.