Castle Story

Castle Story

29 ratings
Decreasing stuttering and freezes
By micha_i
Castle Story is not the best-optimized game.
There are many reasons for occasional stutters while playing, but the largest one is the Garbage Collector.
   
Award
Favorite
Favorited
Unfavorite
What is the Garbage Collector?
When the game works, there are a lot of things going on in your computer.
  • Every time a Bricktron makes a decision to go do a task, it has to put the information about that task somewhere.
  • Every time it wants to go somewhere, it has to calculate the path to go there.
  • Every time a tree is chopped down, a block is mined, a new item has to be created.

All of those things use up memory. However, when they're not needed anymore, that memory can be freed up. That is the job of the Garbage Collector.

The garbage collector works by pausing the game for a moment, cleaning up the memory the game is not using anymore, and unpausing the game back.
However if the game is not optimized, or if it's creating a lot of garbage, the garbage collector will have a hard time keeping up, and the time it takes to clean up the memory will increase.

That is the main source of the stutters you see in Castle Story.
Garbage Collector specifics
Castle Story is written using Unity game engine, which uses the Mono runtime to be able to run the game on Windows, Linux and Mac.

The Mono runtime is at the time an old runtime and doesn't have the modern performance improvements that do exist in runtimes like .NET Core.

However that doesn't mean we have to suffer!

Mono has support for two kinds of Garbage Collectors:
  • The default one, which works fine for basic applications
  • SGen, which is optimized for work that needs consistent throughput
SGen is directed towards applications that should not pause for longer periods of time, such as servers and games.

Additionally, we can change the parameters of the garbage collector, such as:

  • max-heap-size=<megabytes>m
    Makes the Garbage Collector pre-allocate more memory from your computer, which should decrease the frequency of the stutters, as well as fix out-of-memory crashes.
  • major=marksweep-conc-par
    Makes the major collector work concurrently to the application. Can be useful if the game takes a lot of memory (Castle Story does!)
  • MONO_GC_PARAMS=mode=pause:<milliseconds>
    Changes the SGen mode to pause-configurable, and sets the maximum pause time to X milliseconds. Should decrease the frequency of long stutters.
  • nursery-size=<size in bytes>
    The nursery is an area of memory that holds short-term items. By increasing the size of the nursery (default is 4 megabytes) you can decrease the frequency of the stutters, but they can be a bit longer.
  • minor=simple-par
    Makes the nursery collector work concurrently. While usually not changing much, it can improve the performance if you have also increased the size of the nursery.
So... what do I need to do?
First, go to the Castle Story's main directory:
Steam > Library > Castle Story > Manage > Browse Local Files

Next, create a shortcut to the Castle Story.exe file.

Right-click on the shortcut and select Properties.

Then, in the Target field, add this (After the quotes!):
--gc=sgen

The whole Target field should look like this:
"<SteamApps directory>\Castle Story\Castle Story.exe" --gc=sgen

This enables the SGen garbage collector for Castle Story, which should decrease the stutters considerably.

Make sure you open the game using that shortcut (you can move the shortcut to e.g. your Desktop).
By the way, it also disables the annoying launcher.

If you want to fiddle with the Garbage Collector settings, look up "How to set environment variables" on how to setup the variables for the Garbage Collector.
8 Comments
omniw 24 Apr @ 12:02am 
Enraged Koala, thats for sure, bro
Enraged Koala 11 Apr @ 5:42pm 
@Zoomie67 Thanks, that is really useful. Where devs fail, the community can step in. It is a shame though that such a great game was left the way it was though. A thriving modding community would let this game live for a long, long time.
Kirbofir24 23 Dec, 2024 @ 11:45am 
Wow~ such a tiny piece of code may help with my gameplay experience in this game, it's such a shame the devs couldn't optimize it better and add more for the late game runs..
micha_i  [author] 2 Aug, 2024 @ 2:58pm 
@Hansey42
Happy to help!
I am really sad that Castle Story has been abandoned - there is much in the game that could have been optimized and made better in general.
Hansey42 29 Jan, 2024 @ 1:19pm 
For others: This makes stutters in the game manageable. before using this tip the game became more and more unplayable. Not stutter free, but stops the negative decline over time during a game. Thanks alot Micha!

The environment variables of the garbage collector is greek to me, as there are alot of different guides for the garbage collector online.
Zoomie67 14 Jan, 2024 @ 11:09am 
Once the shortcut is setup, you can directly link the shortcut to Steam. (I've tested it)

Steps:
Go to properties in the steam library of Castle Story (the gear icon).
In the launch options under general, you will need to provide a pathway to wherever you have the shortcut. In my case, I placed it in the game folder. So in my case, the pathway I entered was:

"D:\SteamLibrary\steamapps\common\Castle Story\Castle Story.lnk" %command%

Note the quotes and the %command%. %command% instructs Steam to "open" the specified file, while the quoted pathway specifies the place Steam should look.

The "D" you see is the hard drive grade I have the game stored on, so if yours is NOT a D grade, you'll need to replace that with A, B, C, etc. ".lnk" is the file type for a shortcut.
micha_i  [author] 28 Nov, 2022 @ 10:54am 
Added how the target field should look like
Chtoper 28 Nov, 2022 @ 3:17am 
Hello
Can you please show example how should the "target" field look like? It gives errors when I'm trying to put something in there.