Friday, June 13, 2014

FMod - Manage that Memory

One major outstanding issue with Abiathar was that it could not load large level sets on 32-bit computers. The view state caches took up so much room that they would overflow the amount of memory granted to a managed process on an x86 platform. This was impacting one of the Keen community's important modders, so I took another crack at solving it.

First, I looked into the MemoryCache class in the Runtime.Caching assembly. At first, I thought I could easily reorganize the view state dictionaries into a MemoryCache accessed with a string like "1/Background" but then I discovered that almost all the planes need continued access to their cache to render changes efficiently. I did more research on an equivalent of Java's SoftReference, but there is no nice solution that doesn't require me to rework everything.

This was when I noticed that every plane for every level is given a giant bitmap when the dependency file is opened. It was very easy to move all the view state creation into the level and tile palette switch segments. That had a great effect: I could open the Atroxian Realm files in 32-bit mode without crashing. Unfortunately, it would still run out of memory and die if I looked at enough levels.

Then, I thought of a brilliant hack. As long as the represented level is not being viewed, it's possible to delete a level view state, thereby destroying and disposing its graphics resources. Another brilliant hack involved the nature of the out-of-memory error: it's an exception and can be caught - and therefore squashed before it affects the user. With a few invocations of Process static methods from System.Diagnostics, I had a cache deleter wired up to the out-of-memory handler. When System.Drawing throws ArgumentException, Abiathar starts deleting level view states until the working set size is below 1400MB. The executing method can then invoke itself from the exception handler, restarting its work with available memory.

I tested this more, polished it up, and released it as Abiathar v1.4.1. Last advantage of The Omegamatic: gone.

No comments:

Post a Comment