GarbageProtector

8.4k Downloads

Some WoW addons call the lua collectgarbage function irresponsibly, causing all execution to halt until it is finished. This can take more than half a second, which freezes the game in an annoying manner. Most addons don't need to make such calls, but some do anyway, causing these lockups.

Instead of maintaining the removal of collectgarbage calls in all the addons I use, I decided to write this tiny addon which intercepts these irresponsible collectgarbage calls to prevent these chunky lockups and freezes. Now my game feels smoother when performing various actions which previously caused stutters from collectgarbage.

I've also added a hook for UpdateAddOnMemoryUsage, a CPU-intensive function which I found several addons were calling when they weren't even profiling for any reason. With this addition, I figured some people may want the option to disable this hook so they can do memory profiling with GarbageProtector on, so I made CLI and GUI options menus. Plus, if you're an addon author, you can run ToggleGarbageProtector, ToggleHandlecollectgarbage, or ToggleHandleUpdateAddOnMemoryUsage directly to disable the interference of either of GarbageProtector's hooks.

The addon's folder name has 3 "!"s at the beginning to cause it to be loaded first, before any addons that would slow loading screens with collectgarbage calls. There is another addon with 2 "!"s that I wanted GarbageProtector to load before, so that's why 3 instead of 2 or 1.

Warning: Due to the nature of the 2 hooks GarbageProtector implements, unless the hooks are toggled off (from either the GarbageProtector options menu, the command line interface, or the Lua setter functions), it will prevent:

  • full manual garbage collection cycles (function named "collectgarbage"; does not prevent the regular rate of Lua's garbage collector)
  • updating the current count of addon memory usage (function named "UpdateAddOnMemoryUsage"; makes profiling addons show 0 or their most recently-queried value)

For example, ElvUI has a feature to run a garbage collection manually by clicking somewhere. This is prevented by GarbageProtector because it is an irresponsible use of collectgarbage which locks up your game (which if run automatically like some addons do, is not nice to the user). However, if you really want to run it, you can toggle GarbageProtector's option with "/gp collectgarbage" or from the GUI options menu. That's why I added options in the first place: so you can choose what you want to do.